How to reset unused memory to reduce VM snapshot size

On Linux, how can I reset the freed pages of memory to reduce the size of the virtual machine snapshots?

Unusual memory is saved in the picture, even if this memory has been freed and is no longer used. For example, memory pages freed from the file system cache with

sync && echo 3 > /proc/sys/vm/drop_caches 

uselessly stored in the snapshot because they were not reset to zero.

Update . A really terrible solution that seems to work is malloc + memset memory in a loop until it hits the OOM killer. This reduces the size of a single VM snapshot, which I tested from about 800 MB to about 400MB. Is there a better solution?

+7
linux virtual-machine linux-kernel
source share
2 answers

Something like this will free up the disk cache and then allocate & memset the amount of free memory (minus 32 MB, as a margin to avoid swap swapping) using dd. Temporarily create VM un-CoW memory in the case of, for example, KSM, but leave (unallocated) memory blocks filled with zeros, which can then be shared / unlocked with other virtual machines (with KSM) or easily compressed.

 #!/bin/bash echo 3 > /proc/sys/vm/drop_caches memfree=$(free -m | awk '/^Mem/ {print $4-32}') if [ $memfree -gt 0 ]; then dd if=/dev/zero of=/dev/null bs=${memfree}M count=1 fi 
+3
source share

To reduce the size of a virtual machine:

  • Log in to the virtual machine; open a terminal and enter

     cd /home/ sudo dd if=/dev/zero of=foo bs=4096 sudo rm foo 
  • Turn off the virtual machine, go to the VmWare / VmPlayer menu and go to

    vmware โ†’ virtual machine settings โ†’ HD โ†’ utilities โ†’ compact

    and build a virtual machine

If you want to reduce the size even further, use 7-zip. For example, on Linux, you can enter:

 7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on archive.7z directory 
+1
source share

All Articles