-1

I have more than 20 category in which each category contains more than 400+ subcategories in webapplication.

When I press perticular category button it throws java.lang.OutOfMemoryError. Here is java file code. In this code, I am putting subcategory data in to json object and then add it to json array.

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;
} 

Here in each category it may have 400+ subcategory. When I press the category button it shows java.lang.OutOfMemoryError.

How to solve this. I don't want to increase JVM memory for this. I want Java code solution to handle this.

Florent Bayle
  • 9,983
  • 3
  • 31
  • 42
samkit shah
  • 631
  • 7
  • 18
  • 2
    You put the same Categorywisedata for every category in the responseObject. How big is Categorywisedata? How much memory has your JVM allocated? did you try increasing it? – Joram Sep 04 '14 at 06:52
  • 1
    @Joram - question says **I don't want to increase JVM memory for this.** – DavidPostill Sep 04 '14 at 06:54
  • I have said that don't want to increase JVM memory for this. It is enough for small applications but this time I have to deal with big data. So I want Java Code solution for that. – samkit shah Sep 04 '14 at 06:55
  • 2
    You don't want to increase memory, you want to refactor your code. This better suits for [codereview.stackexchange.com](http://codereview.stackexchange.com) – icza Sep 04 '14 at 06:57
  • You can try creating the JSON String using a StringBuilder manually instead of just instantiating a JSONArray and JSONObject inside of the loop to create the JSON String. – shazin Sep 04 '14 at 06:58
  • 1
    possible duplicate of [What is an OutOfMemoryError and how do I debug and fix it](http://stackoverflow.com/questions/24510188/what-is-an-outofmemoryerror-and-how-do-i-debug-and-fix-it) – Raedwald Sep 04 '14 at 06:59
  • @samkitshah I don't understand why you think that increasing the JVM memory is a bad idea ? If you are really dealing with "big data" (8000 entries is not "big data"), and you are storing it in memory you will have to increase the JVM memory, there is no other choice. – Florent Bayle Sep 04 '14 at 07:00
  • Because in my application, I can allocate only limited amount of memory. Not more than that. That's why I have already mentioned that I want Java Code solution,if possible. – samkit shah Sep 04 '14 at 07:02
  • getting no solution at all. – samkit shah Sep 04 '14 at 07:10

1 Answers1

1

What is out in this code?
Is it a web application and you are posting this over http?
You might be able to flush the output stream. If the container supports streaming the content, then it won't build up in a potential buffer causing the out of memory error.

What line is causing the out of memory exception?

An alternative could be to only reply with the categories and ignore the subcategories. If the client needs subcategories for one category, it can request that data. If it needs subcategories for all of them, it will have to request it multiple times.
Yes, this will be slower, but speed and memory are often traded off with each other.

pimaster
  • 1,807
  • 10
  • 11
  • here out is "PrintWriter out = response.getWriter();" an object of PrintWriter. Yes this is a web application. And you are showing the alternative way, actually I am doing that thing. But doesn't happen. – samkit shah Sep 04 '14 at 09:47
  • Thanks for the additional details. From the javadoc "if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output" - Have you tried flushing the output stream? – pimaster Sep 04 '14 at 10:07