Official docs state: https://www.virtualbox.org/manual/ch08.html#vboxmanage-storageattach
VBoxManage storageattach <UUID> --nonrotational:
This switch allows to enable the non-rotational flag for virtual hard disks. Some guests (i.e. Windows 7+) treat such disks like SSDs and don't perform disk fragmentation on such media.
VBoxManage storageattach <UUID> --discard:
This switch enables the auto-discard feature for the virtual hard disks. This specifies that a VDI image will be shrunk in response to the trim command from the guest OS. The following requirements must be met:
The disk format must be VDI.
The size of the cleared area must be at least 1MB.
VirtualBox will only trim whole 1MB blocks. The VDIs themselves are organized into 1MB blocks, so this will only work if the space being TRIM-ed is at least a 1MB contiguous block at a 1MB boundary. On Windows, occasional defrag (with "defrag.exe /D"), or under Linux running "btrfs filesystem defrag" as a background cron job may be beneficial.
Notes: the Guest OS must be configured to issue trim command, and typically this means that the guest OS is made to 'see' the disk as an SSD. Ext4 supports -o discard mount flag; OSX probably requires additional settings. Windows ought to automatically detect and support SSDs - at least in versions 7, 8 and 10. Linux exFAT driver (courtesy of Samsung) supports the trim command.
It is unclear whether Microsoft's implementation of exFAT supports this feature, even though that file system was originally designed for flash.
Alternatively, there are ad hoc methods to issue trim, e.g. Linux fstrim command, part of util-linux package. Earlier solutions required a user to zero out unused areas, e.g. using zerofree, and explicitly compact the disk - only possible when the VM is offline.
So storage defined as:
<AttachedDevice discard="true" nonrotational="true" type="HardDisk">
with FS like Ext4 / Btrfs / JFS / XFS / F2FS / VFAT mounted with
-o discard should work...
UPDATE TRIM support in VirtualBox is still unstable: https://www.virtualbox.org/ticket/16795
See also https://superuser.com/questions/646559/virtualbox-and-ssds-trim-command-support