Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java heap space Exception java.lang.OutOfMemoryError

I am developing a plagiarism detection application, which involves lot of document processing. For indexing the documents I am using Apache Lucene and I am using Apache solr as a document server. I am using Jasper reporting as a reporting module. After processing all the documents and detecting plagiarism I am calling an interface developed using Jasper reporting and at that time java heap space exception is occurred. I tried to increase heap space in the application but the same exception is still thrown out. Is there any suggestion to help me solve this problem.

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at reportingModule.PeerSearchUI.generateResults(PeerSearchUI.java:901)
at reportingModule.PeerSearchUI.setResultDetails(PeerSearchUI.java:833)
at gui.form.WizardForm.ViewButtonActionPerformed(WizardForm.java:1372)
at gui.form.WizardForm.access$1200(WizardForm.java:50)
at gui.form.WizardForm$13.actionPerformed(WizardForm.java:870)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
like image 968
KasunKP Avatar asked Dec 28 '22 15:12

KasunKP


2 Answers

From your exception, the error is related to heap (not permgen or any other specific spaces) so the only parameter to tune will be -Xmx and so here are my suggestions :

Have the JVM to use more memory using the -Xmx - try 1G : -Xmx1024m

If that ends up throwing the same error,

  • Start JVM with argument -XX:+HeapDumpOnOutOfMemoryError this will give you a heap dump when program goes in to OOM.
  • Use a tool like visualVM to analyze the heap dump obtained. That will help in identifying memory leaks.

Other than this, there is no generic solution to fix java.lang.OutOfMemoryError: Java heap space - It can be very much application specific.

like image 145
ring bearer Avatar answered Jan 03 '23 08:01

ring bearer


It would help if you would tell us what the variable types you are using to store the document index and reported results.

Try running the reporting app in a separate JVM from the document indexing process.

Indexing documents is a resource intensive operation and if it is holding all the memory used during indexing and then you try to query the index for your report, it can consume even more.

Let's look at the stack trace from the top. The first three lines (read in reverse order) show the smoking gun.

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space at     
java.util.Arrays.copyOf(Arrays.java:2760) at java.util.Arrays.copyOf(Arrays.java:2734) at 
java.util.ArrayList.ensureCapacity(ArrayList.java:167) at java.util.ArrayList.add(ArrayList.java:351) at 

The JVM

  1. checked the size of an array to see if it was big enough
  2. attempts to copy an array (which is the default behavior when growing a Vector)
  3. exhausts available memory and throws the OutOfMemoryError exception

The stack trace doesn't mention the exact array but it has to be a large one.

Question: How are you storing the completed index and where are you putting the report results? Since you haven't said what structure, I'll assume for now a Vector which, is convienent to use when you don't know how much data you have, but is also very wasteful when its capacity is increased. If not told otherwise, it will double in size each time it needs more room so a initial size of 100 bytes quickly can turn into tens of megabytes which almost half of the new room empty.

The next two lines are from your reporting code. I would assume that the reporting module part of the UI has a reference to the array/vector/list being filled with data. Tell us what type it is.

reportingModule.PeerSearchUI.generateResults(PeerSearchUI.java:901) at  
reportingModule.PeerSearchUI.setResultDetails(PeerSearchUI.java:833) at 

The line below implies the exception happens after you've clicked a button, 'View Results' possibly, which kicks off a report.

gui.form.WizardForm.ViewButtonActionPerformed(WizardForm.java:1372) at 

This implies that the results are available and are waiting to be displayed. If that is true, then you need to have a better way to isolate the results.

How about showing only 1 page of results at a time. Only show the first 10 rows, then show the next 10, etc.

like image 44
Kelly S. French Avatar answered Jan 03 '23 07:01

Kelly S. French