If you get this error you may assume it is a lack of memory, which it could be, but more likely it is the default memory size of 256M. The file in this example was about 350M big which of course exceeds the virtual memory limits.
This makes sense in smaller mail servers but if you have large amounts of users or e-mails this limit should be bumped.
doveconf -n | grep -E 'vsz_limit|service imap'
default_vsz_limit = 4 G
service imap-login {
#If you don't see any default size limit then it has defaulted to 256M which is not enough for larger mail servers.
The default will be something like this (even if commented it is still the default or if no default is set)
#default_vsz_limit = 256M
So the total amount of memory consumed by related proceses by dovecot cannot exceed this.
To fix it, uncomment and change 256M to a much larger number like 2048M or 20048M etc.., as long as your server has enough memory to support it.
default_vsz_limit = 4096M
In newer versions of Dovecot usually the /etc/dovecot/conf.d files are ignored:
/etc/dovecot/conf.d/10-master.conf
dovecot, imap, localhost, mailbox, inbox, mmap, var, spool, maildir, index, cache, allocate, memoryif, default, exceeds, virtual, servers, amounts, users, mails, bumped, etc, conf, commented, default_vsz_limit, consumed, proceses, exceed, uncomment, larger, server,