OutOfMemoryError - can I dump data into file instead of memory?
I'm using an API that provides a method like remoteConnection.getObjects()
to get a list of objects from a remo开发者_Go百科te server. The list is sometimes huge and causes a Java heap space OutOfMemoryError. Is there a way of dumping these objects directly to a file rather than have them loaded in memory?
From what you have described, unfortunately the answer is no you cant - "Out of Memory" doesn't actually mean that you have run out of physical memory, it means that your virtual address space is too full / fragmented to allocate any more.
In short - paging bits of memory to disk won't help you because physical memory isn't the bottleneck, the limiting factor is simply the ability to address memory. (The OS will already be seamlessly paging memory to disk for you).
Your options (that I can see) are:
- Lobby the API developer to change their API, or find an alternative way of using the API that returns the data in smaller chunks
- Attempt to increase your available address space by either:
- Reducing your existing memory use as much as possible
- Increasing the address space (e.g. target x64)
unfortunately you can't do much if it's the API returning that amount of data. Can you increase your heap size using the -Xmx jvm arg?
精彩评论