I think your problem is that possibly you are handling more files than your application can handle at a time. So you would need to limit the number of files you get into the memory at any time based on the memory you have with your VM. If your application needs are more than what can be handled by the VM then you can think of these two options
a. Increase the VM size. If you are using 32 bit VM you can not go beyond 3gb, so you would need to migrate to 64 bit VM
b. Other option is to cluster your application.
Hope this helps!!