Java Heap Size Exception

Problem

I was getting this error after setting up a new instance of Tomcat7, using Java 1.7 and running OpenMRS2:

Exception in thread “ActiveMQ Journal Checkpoint Worker” java.lang.OutOfMemoryError: Java heap space
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethod(Class.java:2128)
at java.io.ObjectStreamClass.getInheritableMethod(ObjectStreamClass.java:1442)
Exception in thread “ActiveMQ Broker[localhost] Scheduler” at java.io.ObjectStreamClass.access$2200(ObjectStreamClass.java:72)
at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:510)
at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:472)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:472)
at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:369)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1134)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at org.apache.activemq.store.kahadb.MessageDatabase.checkpointProducerAudit(MessageDatabase.java:1250)
at org.apache.activemq.store.kahadb.MessageDatabase.checkpointUpdate(MessageDatabase.java:1134)
at org.apache.activemq.store.kahadb.MessageDatabase$10.execute(MessageDatabase.java:698)
at org.apache.kahadb.page.Transaction.execute(Transaction.java:728)
at org.apache.activemq.store.kahadb.MessageDatabase.checkpointCleanup(MessageDatabase.java:696)
at org.apache.activemq.store.kahadb.MessageDatabase$3.run(MessageDatabase.java:293)
java.lang.OutOfMemoryError: Java heap space
at java.util.HashMap.newNode(HashMap.java:1734)
at java.util.HashMap.putVal(HashMap.java:630)
at java.util.HashMap.putMapEntries(HashMap.java:514)
at java.util.HashMap.<init>(HashMap.java:489)
at org.apache.activemq.broker.region.AbstractRegion.getDestinationMap(AbstractRegion.java:223)
at org.apache.activemq.broker.region.RegionBroker.getDestinationMap(RegionBroker.java:134)
at org.apache.activemq.broker.region.RegionBroker.purgeInactiveDestinations(RegionBroker.java:922)
at org.apache.activemq.broker.region.RegionBroker$1.run(RegionBroker.java:110)
at org.apache.activemq.thread.SchedulerTimerTask.run(SchedulerTimerTask.java:33)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)

A Java Heap Size error occurs because of certain memory intensive actions (like Data Exporting). The new Spring managed API uses a lot of memory to cache the xml beans. You may need to increase the default memory allocation.

Solution

According to this wiki page, the solution was to adjust the memory settings. Depending on your OS, I was using Ubuntu 16.04.1 LTS.

vimĀ /etc/default/tomcat7

Then change the JAVA_OPTS variable to:

JAVA_OPTS=”-Djava.awt.headless=true -Xmx1024M -Xms1024M -XX:PermSize=256m -XX:MaxPermSize=256m -XX:NewSize=128m”

You can adjust the memory as per your needs, in my case this was multiplying everything by 2:

JAVA_OPTS=”-Djava.awt.headless=true -Xmx2048M -Xms2048M -XX:PermSize=512m -XX:MaxPermSize=512m -XX:NewSize=256m”

Resources

Troubleshooting Memory Errors

Java heap space errors when starting a new installation