This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

ERROR: java.lang.OutOfMemoryError: Java heap space

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 34
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic