• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Out of Memory exception

 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am using the following code to add a file to a jar. It works fine for smaller files, but if the file is over 56MB, it throws and out of memory exception. Any ideas on how I can keep this from happening?
thanks,
-Jason
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could allow the JVM to use more memory when you start your process.
eg.
java -Xmx500m -Xms500m MyClass
The -Xmx option is maximum memory allowed and -Xms is minimum. The above specifies the use of 500MB of memory for the JVM.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another key point would be to use a much smaller buffer, and put it in a loop so it gets reused:

This should use a lot less memory. Note also that read(byte[]) does not necessarily read all bytes in the file at once anyway - you must check the return value to be sure.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic