File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes ERROR: java.lang.OutOfMemoryError: Java heap space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ERROR: java.lang.OutOfMemoryError: Java heap space" Watch "ERROR: java.lang.OutOfMemoryError: Java heap space" New topic
Author

ERROR: java.lang.OutOfMemoryError: Java heap space

Vinith Kumar
Greenhorn

Joined: May 31, 2011
Posts: 2
Hi,

I am facing OutOfMemory exception in Java, Kindly help me to resolve this issue, really appreciate your help

ERROR: java.lang.OutOfMemoryError: Java heap space

CODE:



Thanks in advance...

[Edit - added code tags - MB]

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

The first thing that stands out in your code is that you are allocating a byte array with 50 million bytes in line 9. It looks like you read in a file in lines 12-27 into a byte array, resizing and copying the array to make it larger if the file is even bigger. If you read in a really large file (tens or hundreds of megabytes or bigger) then this can easily lead to an OutOfMemoryError.

To avoid this you could start the JVM with the -Xmx parameter to allow it to allocate more memory. For example: java -Xmx512M org.mypackage.MyProgram will allow it to allocate up to 512 MB RAM.

However, that only taking away the symptom. It would be better to rewrite that code so that it isn't necessary to read the data for the blob into a large buffer in memory. I don't know exactly how to do it right now but as far as I know, when you want to insert a blob in the database, you do not need to have the data in memory - you can pass it an InputStream (for example a FileInputStream) and the JDBC driver will read the data from there to insert it into the database.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

There are tools to visualize the state of heap. The heap should be dumped to a .hprof file at the first occurrence of OutOfMemoryError. Eclipse Memory Analyzer can open .hprof file and you can then visualize the heap. See this post for more details:
http://badalchowdhary.wordpress.com/2011/11/09/java-heap-memory-analyzer/

Thanks,
Badal


Blog: http://badalchowdhary.wordpress.com/
Twitter: http://twitter.com/badalrocks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ERROR: java.lang.OutOfMemoryError: Java heap space