简体   繁体   English

尝试从Web服务获取响应时出现“ Java堆空间”错误

[英]“Java Heap Space” error when I'm trying to get the response from a webservice

Java Heap Space error. Java堆空间错误。 I want to get a large String result from a webpage, please check the example below. 我想从网页上获取较大的String结果,请检查以下示例。 But

String response = resource.get(String.class);

will always return a "java.lang.OutOfMemoryError: Java heap space" error. 将始终返回“ java.lang.OutOfMemoryError:Java堆空间”错误。 The problem is the webpage too large. 问题是网页太大。 Is there a streaming solution or other solution that I could use instead of "String response"? 是否可以使用流式解决方案或其他解决方案代替“字符串响应”?

Thanks. 谢谢。

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

public class helloWorldClient {
public helloWorldClient() {
    super();
}

public static void main(String[] args) {
    Client c = Client.create();
    WebResource resource = c.resource("http://localhost:7101/RESTfulService-Project1-context-root/jersey/helloWorld");
    String response = resource.get(String.class);
    }
}

I have seen this below example in this page https://jersey.java.net/documentation/1.18/client-api.html 我已经在此页面中看到以下示例https://jersey.java.net/documentation/1.18/client-api.html

You can try this: 您可以尝试以下方法:

InputStream in = resource.get(InputStream.class);
// Read from the stream
in.close();

The “java.lang.OutOfMemoryError: Java heap space” error you are facing is triggered when you try to add more data into the heap space area in memory, but the size of this data is larger than the JVM can accommodate in the Java heap space. 当您尝试向内存中的堆空间区域添加更多数据时触发了您遇到的“ java.lang.OutOfMemoryError:Java堆空间”错误,但是此数据的大小大于JVM在Java堆中可以容纳的大小空间。

The heap size is a threshold set at JVM intialization. 堆大小是在JVM初始化时设置的阈值。 If you do not set it yourself, platform-specific default is used. 如果您未自行设置,则使用特定于平台的默认设置。 It is a reasonable safety net to guard against for example leaking processes otherwise bringing the whole machine down to its knees. 这是一个合理的安全网,可以防止泄漏过程,否则会使整个机器跌落到膝盖。

The easiest way to overcome the issue is just to increase (or add, if missing) the following parameter setting the maximum heap size for your java process, similar to the following example where 1GB heap is allowed: 解决此问题的最简单方法是增加(或添加,如果缺少)以下参数,以设置Java进程的最大堆大小,类似于以下允许1GB堆的示例:

java -Xmx1024m com.mycompany.MyClass

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

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