开发者

Java heap memory capacity

开发者 https://www.devze.com 2023-01-15 06:47 出处:网络
While writi开发者_StackOverflow中文版ng huge data to a file using java, how can one avoid java heap memory out of space error from occuring?..This is causing some failures for my program..

While writi开发者_StackOverflow中文版ng huge data to a file using java, how can one avoid java heap memory out of space error from occuring?..This is causing some failures for my program..

Regards,

magggi


Your problem is that you're using a single StringBuffer to handle all your content. You should try to flush your data more often. You will gain performance and memory.

Without the original code I can't help you more, but try to flush your buffer from time to time.


Not advised in your case

If you still want to use your StringBuffer and flush it only once, then you'll have to increase your heap space with the -Xmx***M option on the java command.


Resources :

  • oracle.com - basic IO
  • oracle.com - Java command line tool (see -Xmx part for heap)

On the same topic :

  • java.lang.OutOfMemoryError: Java heap space


From your comments it sounds like you're iterating over some objects that fill up a StringBuffer and, once you're done iterating, you flush the entire StringBuffer to a file. Instead of doing this, you can either write directly to an output stream on each iteration (recommended) or, on each iteration, write to your StringBuffer, flush it to the output stream, and then clear the StringBuffer for the next iteration (not preferred). This should lower your memory usage.

I don't recommend increasing the heap size -- it doesn't sound like the right solution to this problem.


Use the -Xmx parameter when launching the java program to set the maximal heap size:

java -Xmx256M com.foo.MyClass


Did you try passing the memory parameters to the applicaiton when starting it?

For example the following sets the maximum memory to 1 GB

java -Xms512m -Xmx1024m myClass 


A couple of options

  1. Increase Java Heap Size using the -Xmx JVM parameter. After -Xmx, write the number of megabytes your program needs to successfully run.

  2. Use Streaming. Compute the next N bytes in the file and then write them using your FileOutputStream, throwing them away before computing the next N bytes. More code would be necessary to know if and how this would work for your program.

Edit: From what you are saying, it sounds like you could wrap the FileOutputStream in a BufferedOutputStream, drop the StringBuffer entirely and just write directly to the BufferedOutputStream.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号