How to zero unused memory to reduce VM snapshot size
In Linux, how can I zero-out freed memory pages to reduce the size of VM snapshots?
Non-zeroed memory is stored in the snapshot even if that memory has been freed and is no longer used. For example, memory pages freed from the filesystem cache with
sync && echo 3 > /proc/sys/vm/drop_caches
are needlessly stored in the snapshot because they haven't been zeroed.
Update A really horrible solution that appears to work is to malloc+memset memory in a loop until the OOM killer kicks in. This reduces the size of one VM snapshot I tested from about 800MB to about 400MB. Is there a better solution?
Something like this will free up disk cache, then alloc&memset the amount of free memory (minus 32MB, as headroom to avoid hitting swap) using dd. Will temporarily make the VM un-CoW memory in case of for example KSM, but will leave (unallocated) memory blocks filled with zeroes, which can then be shared/deduped with other VMs (with KSM) or easily be 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
To reduce the size of a virtual machine:
Log into the virtual machine; open a terminal and type
cd /home/
sudo dd if=/dev/zero of=foo bs=4096
sudo rm foo
Shut down the virtual machine, enter the VmWare/VmPlayer menu and go to
vmware -> virtual machine settings -> HD -> utilities -> compact
and compact the virtual machine
If you want to further reduce the size, use 7-zip. For example, on Linux, you can type:
7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on archive.7z directory
链接地址: http://www.djcxy.com/p/75220.html
上一篇: 在正在测试的同一类中模拟私有方法
下一篇: 如何清零未使用的内存以减少虚拟机快照大小