Reputation: 21
Java Heap Space error. I want to get a large String result from a webpage, please check the example below. But
String response = resource.get(String.class);
will always return a "java.lang.OutOfMemoryError: Java heap space" error. 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);
}
}
Upvotes: 0
Views: 3525
Reputation: 444
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.
The heap size is a threshold set at JVM intialization. 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 -Xmx1024m com.mycompany.MyClass
Upvotes: 1
Reputation: 391
I have seen this below example in this page 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();
Upvotes: 0