File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Out of Memory exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Out of Memory exception" Watch "Out of Memory exception" New topic

Out of Memory exception

Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
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?

Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="" target="_blank" rel="nofollow"></a>
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
You could allow the JVM to use more memory when you start your process.
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

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Out of Memory exception
It's not a secret anymore!