Transparent Page Sharing basics
In ESX, a 4KB page backing any guest page with content identical to another 4KB guest page can be shared regardless of when, where, and how those contents are generated. ESX periodically scans the content of guest physical memory for sharing opportunities. For each candidate page, a hash value is computed based on its content. The hash value is then used as a key to look up a global hash table, in which each entry records a hash value and the physical page number of a shared page. If the hash value matches an existing entry, a full bit-by-bit comparison of the page contents between the candidate page and the shared page is performed to exclude a false match. After a successful content match, the guest-physical to host-physical mapping of the candidate page is changed to the shared host-physical page, and the redundant host memory copy is reclaimed.
Transparent Page Sharing with large pages
In hardware-assisted memory virtualization systems, ESX will preferentially back guest physical pages with large host physical pages (2MB contiguous memory region instead of 4KB for regular pages) for better performance. If there is not a sufficient 2MB contiguous memory region in the host (for example, due to memory over commitment or fragmentation), ESX will still back guest memory using small pages (4KB). ESX will not share large physical pages because:
However, ESX still generates hashes for the 4KB pages within each large page during page scanning.
In the cases where host memory is overcommitted, ESX may have to swap out pages. Since ESX will not swap out large pages, during host swapping, a large page will be broken into small pages. ESX tries to share those small pages using the pre-generated hashes before they are swapped out. The motivation of doing this is that the overhead of breaking a shared page is much smaller than the overhead of swapping in a page if the page is accessed again in the future.