Gautam Kumar
Gautam Kumar

Reputation: 983

Send huge json object from Java Servlet

Java servlet returns JSON object.

response.setContentType("application/json");
response.getWriter().write(json.toString()); 

JSON object contains data fetched from table (database) size of which > 50 MB.

On running, servlet throws this error:

java.lang.OutOfMemoryError: Java heap space

It seems the issue is while writing json data. Server is unable to allocate contiguous memory of size> 50 MB to the String.

I am unable to figure out fix for this issue. How can I send huge JSON object from Servlet?

Upvotes: 1

Views: 1873

Answers (2)

Ivo
Ivo

Reputation: 444

Splitting the JSON data structure into smaller parts is definitely one way to solve the problem at hand. But an alternative via heap size increase might do the job in this case as well.

The “java.lang.OutOfMemoryError: Java heap space” error will be 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.

Note that JVM gets a limited amount of memory from the OS, specified during startup. There are several startup parameters controlling the separate regions of memory allocated, but in your case you are interested in heap region, which you can set (or increase if present) similar to the following example setting your heap size to 1GB:

java -Xmx1024m com.mycompany.MyClass

Upvotes: 1

Axel Amthor
Axel Amthor

Reputation: 11096

json.toString() is likely to cause the error. It is creating one big string from the already existing json object before anything has been send out.

Slurping everything into memory is convenient, but not very wise when it comes to any limitations. Process your database records one by one and stream immediately to the client instead of copying around in memory. Rule of thumb: "Any limitation given will be exceeded at some time."

Upvotes: 5

Related Questions