Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.StackOverflowError when using Kryo to serialize objects with references to each other

I have a graph-like object that I'm sending from server to client that contains nodes that have adjacencyLists.

I have something similar to this:

Clearing c1 = new Clearing(1, 134, 151);
Clearing c6 = new Clearing(6, 250, 88);

c1.adjacentByPath.add(new Path(1, c6));
c6.adjacentByPath.add(new Path(1, c1));

Each time I send the object that contains these clearings, I receive the following error:

Exception in thread "Server" java.lang.StackOverflowError
at com.esotericsoftware.kryo.Kryo.getRegistration(Kryo.java:448)
at com.esotericsoftware.kryo.util.DefaultClassResolver.writeClass(DefaultClassResolver.java:79)
    ......

Is there a workaround for this in Kryonet? Thanks

like image 363
Abe Fehr Avatar asked Feb 21 '15 00:02

Abe Fehr


1 Answers

It was late answer, but recently I encountered this and was able to replicate as well as fix.


This happens whenever large object graph is used in kryo serialization...

Fix :
1) Try to fix this by tail recursion examples here
2) Increase stack size by specifying Xss in vm options

-Xss1m( or increase as per your requirements) as suggested by Esoteric software documentation which developed Kryo

Very large object graphs :

Stack size

The serializers Kryo provides use the call stack when serializing nested objects. Kryo does minimize stack calls, but for extremely deep object graphs, a stack overflow can occur. This is a common issue for most serialization libraries, including the built-in Java serialization. The stack size can be increased using -Xss, but note that this is for all threads. Large stack sizes in a JVM with many threads may use a large amount of memory.

Imp Note for apache spark + Kryo users:

In case of spark I set SparkConf object spark.executor.extraJavaOptions=... (along with -Xmx -XX options) programmatically or you can also mention that in spark-default.conf

like image 138
Ram Ghadiyaram Avatar answered Sep 23 '22 04:09

Ram Ghadiyaram