VirtualBox decrease disk size

Host OS — Windows. Guest OS — Linux

Unmount disk which need to resize or start from LiveCD.

On VM:

sudo apt-get install zerofree
sudo zerofree /dev/sda1

# /dev/sda1 - your partition

On host Windows:

cd C:\Program Files\Oracle\VirtualBox
VBoxManage modifyhd <path\to\vdi_file.vdi> --compact

Host OS and guest OS — Linux

On guest:

# fill zeros all free space
# 'bs=' - sector size
dd if=/dev/zero of=zero bs=512k

# remove zero file
rm zero

On host:

vboxmanage modifyhd </path/to/thedisk.vdi> --compact