简体   繁体   English

使用GSON的Java堆空间

[英]Java heap space with GSON

I'm creating some big JSON file using GSON to create custom JSON from GTFS (Google Transit). 我正在使用GSON创建一些大的JSON文件,以便从GTFS(Google Transit)创建自定义JSON。 The problem is when I'm converting from my object class to JSON: 问题是当我从对象类转换为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: 我试图从此更改eclipse.ini:

-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? 有任何想法吗?

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. 除了调整JVM外,最好使用流API而不是在编写之前将整个json加载到内存中。

have a look at the 'Mixed Writes Example' on https://sites.google.com/site/gson/streaming 看看https://sites.google.com/site/gson/streaming上的“混合写示例”

I think you should not tweak eclipse.ini because that affects the JVM settings of the Eclipse program. 我认为您不应该调整eclipse.ini因为这会影响Eclipse程序的JVM设置。 I suppose your program starts a different JVM and you should set the memory settings of that JVM. 我想您的程序启动了另一个JVM,您应该设置该JVM的内存设置。 See the "Run configuration" of your program in Eclipse. 请参阅Eclipse中程序的“运行配置”。

The problem is not that the JVM that is running eclipse (controlled through eclipse.ini ) is running out of memory but the separate JVM that is used for running your program is running out of memory. 问题不在于运行eclipse(通过eclipse.ini控制)的JVM的内存不足,而是用于运行程序的单独JVM的内存不足。

If you are using a 1.5 client JVM, I think the standard maximum memory setting ( -Xmx ) is 64 MB. 如果使用1.5客户端JVM,我认为标准最大内存设置( -Xmx )为64 MB。

In order to change the maximum memory, do the following: 为了更改最大内存,请执行以下操作:

  • right-click "Run As..." or "Debug As..." and 右键单击“运行方式...”或“调试方式...”,然后
  • then "Run Configurations" or "Debug Configurations" 然后“运行配置”或“调试配置”
  • then click the arguments tab 然后单击参数选项卡
  • then add -Xmx512m into the "VM arguments" 然后将-Xmx512m添加到“ VM参数”
  • then click run 然后点击运行

If you're still running out of memory, increase the amount. 如果仍然没有足够的内存,请增加内存量。

I think your JVM running in eclipse is out of memory.so you must increase size for JVM.To change the VM for Eclipse you can change the amount of the MV from Windows> Preferences> Java> Installed JREs from there select the JRE and click edit, then write in the Default VM Arguments: to -Xmx1024M or any other amount of memory. 我认为您的Eclipse中运行的JVM内存不足,因此您必须增加JVM的大小。要更改Eclipse的VM,可以从Windows>首选项> Java>已安装的JRE中更改MV的数量,从中选择JRE并单击编辑,然后将默认VM参数写入:-Xmx1024M或任何其他内存量。

Refer this link you got an idea . 参考此链接,您有一个主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM