java.lang.OutOfMemoryError: Java heap space

Dear Sir,

We have a production platform where over low traffic a session dump has been executed. We got the following exception and the customer wants to know its meaning:

[tpaadmin@AVPR-SPS-ME-oame-1 ~]$ db-list -l sessions -o /opt/tpa/logs/
You are about to start the dump of sessions from the database.
The operation may take time. Please ensure adequate disk space in the output
directory. Continue (y/n)?y
Set listed object to: sessions
Set site option to: primary
Set Namespace option to: all
Set 'Output Directory' to /opt/tpa/logs/
Set the current site's Id to: 3

Output directory created: /opt/tpa/logs//SESSIONS-2019-09-30_00_30_44_837/
'4' output files initialized.
Executor service created with '4' threads. Starting to process records.
Fetching records in namespace 'spspd1'
Data file writing is in progress. 101,000 records processed so far...
Exception in thread "tend" Exception in thread "pool-2-thread-1" java.lang.OutOfMemoryError: Java heap space
        at java.net.AbstractPlainSocketImpl.create(AbstractPlainSocketImpl.java:108)
        at java.net.Socket.createImpl(Socket.java:457)
        at java.net.Socket.getImpl(Socket.java:520)
        at java.net.Socket.setTcpNoDelay(Socket.java:980)
        at com.aerospike.client.cluster.Connection.<init>(Connection.java:69)
        at com.aerospike.client.cluster.Node.refresh(Node.java:127)
        at com.aerospike.client.cluster.Cluster.tend(Cluster.java:354)
        at com.aerospike.client.cluster.Cluster.run(Cluster.java:317)
        at java.lang.Thread.run(Thread.java:748)
Exception in thread "Aerospike-3" Exception in thread "pool-2-thread-2" Exception in thread "Aerospike-2" java.lang.OutOfMemoryError: Java heap space
        at org.codehaus.jackson.map.introspect.AnnotatedClass._collectRelevantAnnotations(AnnotatedClass.java:834)
        at org.codehaus.jackson.map.introspect.AnnotatedClass._constructCreatorMethod(AnnotatedClass.java:819)
        at org.codehaus.jackson.map.introspect.AnnotatedClass.resolveCreators(AnnotatedClass.java:375)
        at org.codehaus.jackson.map.introspect.BasicClassIntrospector.classWithCreators(BasicClassIntrospector.java:187)
        at org.codehaus.jackson.map.introspect.BasicClassIntrospector.collectProperties(BasicClassIntrospector.java:157)
        at org.codehaus.jackson.map.introspect.BasicClassIntrospector.forSerialization(BasicClassIntrospector.java:96)
        at org.codehaus.jackson.map.introspect.BasicClassIntrospector.forSerialization(BasicClassIntrospector.java:16)
        at org.codehaus.jackson.map.SerializationConfig.introspect(SerializationConfig.java:973)
        at org.codehaus.jackson.map.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:251)
        at org.codehaus.jackson.map.ser.StdSerializerProvider._createUntypedSerializer(StdSerializerProvider.java:782)
        at org.codehaus.jackson.map.ser.StdSerializerProvider._createAndCacheUntypedSerializer(StdSerializerProvider.java:735)
        at org.codehaus.jackson.map.ser.StdSerializerProvider.findValueSerializer(StdSerializerProvider.java:344)
        at org.codehaus.jackson.map.ser.impl.PropertySerializerMap.findAndAddSerializer(PropertySerializerMap.java:39)
        at org.codehaus.jackson.map.ser.std.MapSerializer._findAndAddDynamic(MapSerializer.java:403)
        at org.codehaus.jackson.map.ser.std.MapSerializer.serializeFields(MapSerializer.java:257)
        at org.codehaus.jackson.map.ser.std.MapSerializer.serialize(MapSerializer.java:186)
        at org.codehaus.jackson.map.ser.std.MapSerializer.serialize(MapSerializer.java:23)
        at org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:122)
        at org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:71)
        at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
        at org.codehaus.jackson.map.ser.std.MapSerializer.serializeFields(MapSerializer.java:262)
        at org.codehaus.jackson.map.ser.std.MapSerializer.serialize(MapSerializer.java:186)
        at org.codehaus.jackson.map.ser.std.MapSerializer.serialize(MapSerializer.java:23)
        at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
        at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
        at org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2575)
        at org.codehaus.jackson.map.ObjectMapper.writeValueAsString(ObjectMapper.java:2097)
        at com.alcatel.tpapps.eocstools.listdb.SessionRecordProcessor.lambda$writeSessionJSON$0(SessionRecordProcessor.java:175)
        at com.alcatel.tpapps.eocstools.listdb.SessionRecordProcessor$$Lambda$8/1556958977.accept(Unknown Source)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at com.alcatel.tpapps.eocstools.listdb.SessionRecordProcessor.writeSessionJSON(SessionRecordProcessor.java:169)
        at com.alcatel.tpapps.eocstools.listdb.SessionRecordProcessor.run(SessionRecordProcessor.java:142)
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space

The last command is still processing!
Please wait a few seconds to run the next command!

It took 3621 seconds to complete this task ...
[tpaadmin@AVPR-SPS-ME-oame-1 ~]$

Thanks!

This is a runtime error in Java which occurs when you allocate a new object in your application over a period of time continuously and the Garbage Collector (GC) cannot make space available to accommodate a new object, and the heap cannot be expanded further, which resulted this error.

Therefore you pretty much have the following options:

  • Find the root cause of memory leaks with help of profiling tools like MAT, Visual VM , jconsole etc. Once you find the root cause, You can fix this memory leaks.
  • Optimize your code so that it needs less memory, using less big data structures and getting rid of objects that are not any more used at some point in your program.
  • Increase the default memory your program is allowed to use using the -Xmx option (for instance for 1024 MB: -Xmx1024m). By default, the values are based on the JRE version and system configuration.

Increasing the heap size is a bad solution, 100% temporary, because you will hit the same issue if you get several parallel requests or when you try to process a bigger file. To avoid OutOfMemoryError, write high performance code:

  • Use local variables wherever possible.
  • Release those objects which you think shall not be needed further.
  • Avoid creation of objects in your loop each time.