I am fairly sure the problem is to do with RAM size rather than the raspberry pi per-se. 4000 messages takes up a lot of space - and upas stores messages in RAM.
Personally I save needed mail messages in named archives and try to keep the number of messages in my inbox doen to the 10s. Erik has nupas which reworks much to keep a file per message on disk, though I'am not sure how thsi works with gmail (imap4 I assume). -Steve