3 out of 4 rated this helpful - Rate this topic

Closing a File Mapping Object

When a process has finished with the file mapping object, it should destroy all file views in its address space by using the UnmapViewOfFile function for each file view.

Unmapping a mapped view of a file invalidates the range occupied by the view in the address space of the process and makes the range available for other allocations. It removes the working set entry for each unmapped virtual page that was part of the working set of the process and reduces the working set size of the process. It also decrements the share count of the corresponding physical page.

Modified pages in the unmapped view are not written to disk until their share count reaches zero, or in other words, until they are unmapped or trimmed from the working sets of all processes that share the pages. Even then, the modified pages are written "lazily" to disk; that is, modifications may be cached in memory and written to disk at a later time. To minimize the risk of data loss in the event of a power failure or a system crash, applications should explicitly flush modified pages using the FlushViewOfFile function.

When each process finishes using the file mapping object and has unmapped all views, it must close the file mapping object's handle and the file on disk by calling CloseHandle. These calls to CloseHandle succeed even when there are file views that are still open. However, leaving file views mapped causes memory leaks.

 

 

Send comments about this topic to Microsoft

Build date: 2/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Translate to chinese language

When a process has finished with the file mapping object, it should destroy all file views in its address space by using the UnmapViewOfFile function for each file view.

當某個程序持有檔案對應(file mapping)物件並且在完成工作時,它必須針對每一個檔案檢視(file view)使用UnmapViewOfFile函式,來銷毀所有在位址空間的檔案檢視(file view)。

Unmapping a mapped view of a file invalidates the range occupied by the view in the address space of the process and makes the range available for other allocations. It removes the working set entry for each unmapped virtual page that was part of the working set of the process and reduces the working set size of the process. It also decrements the share count of the corresponding physical page.

一個對應檔案的取消映射,讓佔用行程位址空間裡的範圍檢視無效,並且讓這範圍能夠分配出去。它移除了每一個取消映射虛擬分頁的工作集(working set)入口,入口是行程工作集的一部分,減少了行程的工作集大小。它也會遞減相對應實體分頁的共享計數器。

Modified pages in the unmapped view are not written to disk until their share count reaches zero, or in other words, until they are unmapped or trimmed from the working sets of all processes that share the pages. Even then, the modified pages are written "lazily" to disk; that is, modifications may be cached in memory and written to disk at a later time. To minimize the risk of data loss in the event of a power failure or a system crash, applications should explicitly flush modified pages using the FlushViewOfFile function.

修改過的分頁在取消映射檢視中,不會寫入到磁碟中,一直到它的共享計數器數字成為0,或換句話說,直到它從所有行程工作集的共享分頁中取消映射或整頓後。即時是這樣,修改過的分頁"延遲(lazily)"寫入磁碟,它的修改可能是快取在記憶體,稍後才會寫入到磁碟。為了讓電源故障或系統崩潰後資料遺失的風險最小化,應用程式必須明確地的使用FlushViewOfFile函式來刷新修改過的分頁。

When each process finishes using the file mapping object and has unmapped all views, it must close the file mapping object's handle and the file on disk by calling CloseHandle. These calls to CloseHandle succeed even when there are file views that are still open. However, leaving file views mapped causes memory leaks.

當每一個程序完成使用檔案對應物件並且取消映射所有檢視後,必須呼叫CloseHandle來關閉檔案對應物件及磁碟中檔案的控制代碼(handle)。這些呼叫在檔案檢視仍然開啟下,仍然會CloseHandle成功。無論如何,留著檔案檢視對應會造成記憶體洩漏。