My favorites | Sign in
Project Home Downloads Wiki Issues Source
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 59: toJSONString crahses for large number of objects in JSONArray
2 people starred this issue and may be notified of changes. Back to list
Status:  WontFix
Owner:  ----
Closed:  Aug 2013


Sign in to add a comment
 
Reported by vishalghadge, Nov 6, 2011
What steps will reproduce the problem?
1. Build jsonObject containing JSONArray with around 2000 json objects [in short large number of objects in JSONArray] 
2. Convert it into string using toJSONString of main jsonObject
3. it crashes on Android 

What is the expected output? What do you see instead?
It should not crash

What version of the product are you using? On what operating system?


Please provide any additional information below.
11-07 12:20:48.180: ERROR/dalvikvm-heap(21506): Out of memory on a 2235124-byte allocation.
11-07 12:21:11.420: ERROR/AndroidRuntime(21506): FATAL EXCEPTION: Thread-44
11-07 12:21:11.420: ERROR/AndroidRuntime(21506): java.lang.OutOfMemoryError
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:96)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:147)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at java.lang.StringBuffer.append(StringBuffer.java:219)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at java.io.StringWriter.write(StringWriter.java:148)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONObject.writeJSONString(JSONObject.java:75)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONObject.writeJSONString(JSONObject.java:86)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONValue.writeJSONString(JSONValue.java:153)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONObject.writeJSONString(JSONObject.java:78)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONObject.writeJSONString(JSONObject.java:86)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONValue.writeJSONString(JSONValue.java:153)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONArray.writeJSONString(JSONArray.java:62)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONArray.writeJSONString(JSONArray.java:70)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONValue.writeJSONString(JSONValue.java:153)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONObject.writeJSONString(JSONObject.java:78)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONObject.writeJSONString(JSONObject.java:86)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONValue.writeJSONString(JSONValue.java:153)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONObject.writeJSONString(JSONObject.java:78)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at org.json.simple.JSONObject.writeJSONString(JSONObject.java:86)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at com.myapp.jsonToString(abc.java:61)


Nov 6, 2011
#1 vishalghadge
tried following code.. which was more efficient but it also crashes for more objects.
		
final Writer writer = new StringWriter();

		try
		{
			json.writeJSONString(writer);
		}
		catch ( final IOException e )
		{
			if ( QOCCompileConstants.LOGGING )
			{
				LOG.log(Level.ERROR, "IO error" + e.getMessage());
			}
		}
		return writer.toString();
Nov 28, 2011
Project Member #2 fangyid...@gmail.com
Please make sure you that you allocate sufficient memory for JVM, or use the streaming approach (what you mentioned in the comment):
https://code.google.com/p/json-simple/wiki/EncodingExamples#Example_2-4_-_Encode_a_JSON_array_-_Using_List_and_streaming

Please note that a StringWriter still stores JSON output in the memory. Please use something like FileWriter or your own writer implementation.

Aug 9, 2013
Project Member #3 jon.cham...@gmail.com
With respect, I don't think this is really a bug with json.simple; trying to create a String that's too big to fit in available memory by any means will cause the same problem.
Status: WontFix
Sign in to add a comment

Powered by Google Project Hosting