Comments (8)
You don't have to convert the stream into a String to pass it to JSONParser.
JSONParser accepts a Reader as its parameter.
Could you comment out the last line and then test it again?
return (JSONObject) jsonParser.parse(sb.toString());
And could you help to print out the result of sb.toString() and post it here
before
passing it to JSONParser? That will be very helpful to analyze this issue.
Thanks.
Original comment by [email protected]
on 28 Nov 2009 at 4:10
from json-simple.
That didn't help, unfortunately. I also can't log the StringBuilder contents,
because
adb just cuts off strings which are too long to log, but I've attached the JSON
data
served by our web service.
It's not a problem with that specific document though. I've seen this happening
all
over the place.
Original comment by [email protected]
on 30 Nov 2009 at 9:20
Attachments:
from json-simple.
Thanks a lot for the data.
It's strange that from my initial testing (using a very simple method), nothing
seems
to be special.(Local JDK is 1.6. What is the version of the JDK/JRE in your
environment?)
Here's the code:
{{{{
public static void main(String[] args) throws Exception{
long t = System.currentTimeMillis();
long m = Runtime.getRuntime().freeMemory();
Object obj = JSONValue.parse(new FileReader("reviews.json"));
long delta = System.currentTimeMillis() - t;
long deltaM = m - Runtime.getRuntime().freeMemory();
System.out.println(delta + "ms");
System.out.println(deltaM/1024 + "K");
System.out.println(obj);
}
}}}}
Here's the result:
79ms
537K
(The resulting JSON text)
I also used YourKit 7.5 to do memory profiling and got the following data:
Overall objects:
All live objects: 13,025 Shallow size: 1,098,824 bytes Retained size: 1,098,824
bytes
Dominant objects:
char[] 2,902 objects, shallow size: 429,656
java.lang.String 3,132 objects, shallow size: 75,168
Thanks.
Original comment by [email protected]
on 30 Nov 2009 at 11:07
from json-simple.
I just replaced JSON simple with the reference JSON implementation from json.org
(which is shipped with Android), and it doesn't suffer from this problem.
There must be something leaking (and massively so -- considering it turns a 40k
JSON
string into 12 megabytes), in JSONParser.parse().
However, to not waste your time:
It may well be that this is in fact something related to Apache Harmony, the
open-source Java implementation that powers Android. I have been running into
problems on Android several times now where Harmony turned out to be the source
of
the problem (it's scary sometimes how buggy it can be at its core
infrastructure).
I guess the best thing to do would be:
Write a simple test case which parses a JSON file, and profile it both using
the Sun
JVM and the Harmony JVM.
The Android profiler unfortunately isn't detailed enough to tell me exactly
which
method/object is actually consuming so much memory, but it shows me 10-12MB
worth of
character data being allocated during a parse(), so it must be something
related to
reading/buffering character data.
Original comment by [email protected]
on 30 Nov 2009 at 11:11
from json-simple.
Thanks a lot for your information.
I'll test it in Harmony JVM.
Original comment by [email protected]
on 30 Nov 2009 at 11:20
from json-simple.
One solution would be to just use the lexer. I've posted some code to Fang that
extends the lexer and uses it
similar to the XMLStreamReader in Java 1.6, effectivly saving lots of RAM when
processing large amounts of
JSON. Let me know and I'll post it again.
Original comment by [email protected]
on 30 Nov 2009 at 4:59
from json-simple.
Hi,
The following patch should fix the OutOfMemory problem. At least, it works for
my JVM.
--- a/src/org/json/simple/parser/Yylex.java
+++ b/src/org/json/simple/parser/Yylex.java
@@ -577,7 +577,7 @@ int getPosition(){
}
case 25: break;
case 4:
- { sb.delete(0, sb.length());yybegin(STRING_BEGIN);
+ { sb = null; sb = new StringBuffer(); yybegin(STRING_BEGIN);
}
case 26: break;
case 16:
Glen
Original comment by [email protected]
on 16 May 2012 at 7:25
from json-simple.
Hi glen.tw,
I've applied it to the code. Thanks a lot for your contribution.
Unfortunately I cannot validate it on Android Phone because I don't have one.
Can any of you help validate it and let me know the result?
Thanks,
Yidong.
Original comment by [email protected]
on 16 May 2012 at 1:56
- Changed state: Fixed
from json-simple.
Related Issues (20)
- escaped backslash results in new line character if followed by 'n' or 'r' HOT 1
- JSONParser.parse() throws a parse exception on trailing and leading whitespace of a JSON String. HOT 1
- Make junit's scope to test HOT 3
- Build JSONArray from a Collection HOT 1
- StringBuilder and StringBuffer values are unquoted HOT 1
- I need to modify JSON object value at run time.
- Fork this project HOT 4
- A wrapper for converting A Java HashMap<Object, Object> containing Arrays to a correctly represented JSONObject HOT 1
- JSONParser.parse not working for UTF-8 encoded file HOT 4
- linkedHashMap does not preserve order once data has reached client
- Please provide a source archive for release 1.1.1
- MAVEN, json-simple 1.1.1 depends on Junit HOT 3
- bug in get(), ContainerFactory HOT 1
- JSONObject produces warning in code HOT 6
- Long cast error, even if String used HOT 2
- Patch for /trunk/src/main/java/org/json/simple/parser/ContainerFactory.java HOT 2
- nothing is being converted to a JSONObject in my java class, throwing an exception HOT 1
- Building 1.1.1 with java-1.8.0 tests fail - hash not matching? HOT 2
- Functions are missing from json_simple.jar (1.1)
- should not always use toString() as fallback in JSONValue.writeJSONString() HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-simple.