Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java heap space with GSON

I'm creating some big JSON file using GSON to create custom JSON from GTFS (Google Transit). The problem is when I'm converting from my object class to JSON:

  Gson gson = new Gson();
  String rutasJSON = gson.toJson(my_object);

It gives this exception:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
    at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
    at java.lang.AbstractStringBuilder.append(Unknown Source)
    at java.lang.StringBuffer.append(Unknown Source)
    at java.io.StringWriter.write(Unknown Source)
    at com.google.gson.stream.JsonWriter.string(JsonWriter.java:544)
    at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:387)
    at com.google.gson.stream.JsonWriter.value(JsonWriter.java:402)
    at com.google.gson.internal.bind.TypeAdapters$13.write(TypeAdapters.java:353)
    at com.google.gson.internal.bind.TypeAdapters$13.write(TypeAdapters.java:337)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:105)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.Gson.toJson(Gson.java:586)
    at com.google.gson.Gson.toJson(Gson.java:565)
    at com.google.gson.Gson.toJson(Gson.java:520)
    at com.google.gson.Gson.toJson(Gson.java:500)
    at transporte.main(transporte.java:152)

I've tried to change eclipse.ini from this:

-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.100.v20110502
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m

To this:

-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.100.v20110502
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
1024M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
--launcher.defaultAction
openFile
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms512m
-Xmx512m

Any ideas?

like image 212
mram888 Avatar asked Jan 29 '26 18:01

mram888


2 Answers

Instead of tweaking the JVM it might be a good idea to use a streaming API instead of loading the whole json into memory before writing it.

have a look at the 'Mixed Writes Example' on https://sites.google.com/site/gson/streaming

like image 78
Jonas Adler Avatar answered Feb 01 '26 08:02

Jonas Adler


I think you should not tweak eclipse.ini because that affects the JVM settings of the Eclipse program. I suppose your program starts a different JVM and you should set the memory settings of that JVM. See the "Run configuration" of your program in Eclipse.

like image 42
jabal Avatar answered Feb 01 '26 06:02

jabal