-1

我有 20 多个类别,其中每个类别包含 webapplication 中的 400 多个子类别。

当我按下特定类别按钮时,它会抛出java.lang.OutOfMemoryError. 这是java文件代码。在这段代码中,我将子类别数据放入 json 对象,然后将其添加到 json 数组中。

String responceObjectStr;
JSONObject responseObject;
for(int i=0;i<category.length;i++){
    responseObject = new JSONObject();
    JSONArray ja = new JSONArray();
    responseObject.put(category[i],Categorywisedata);//adding data to response
    ja.put(responseObject);//putting response object to JSONArray
    responceObjectStr = new String(ja.toString().getBytes("UTF-8"));
    out.print(responceObjectStr);//response
    responceObjectStr = null;
    responseObject = null;
} 

在每个类别中,它可能有 400 多个子类别。当我按下类别按钮时,它显示java.lang.OutOfMemoryError.

如何解决这个问题。我不想为此增加 JVM 内存。我想要 Java 代码解决方案来处理这个问题。

4

1 回答 1

1

out这段代码是什么?
它是一个 Web 应用程序,您是通过 http 发布的吗?
您可能能够刷新输出流。如果容器支持流式传输内容,则它不会在潜在缓冲区中堆积,从而导致内存不足错误。

哪一行导致内存不足异常?

另一种方法是只回复类别而忽略子类别。如果客户端需要一个类别的子类别,它可以请求该数据。如果它需要所有子类别,则必须多次请求。
是的,这会更慢,但速度和内存通常是相互权衡的。

于 2014-09-04T07:21:48.277 回答