0

I am trying to parse a json file of 5mb, which is causing out of memory exceptions, I know the solution for this is to use json stream parser,

But I need to know why this is happening when Android devices has 1GB ram, at least it can allocate 100MB to my application I think, what going on?

Sandeep Manne
  • 6,030
  • 5
  • 39
  • 55

3 Answers3

4

Many other apps will claim their portion of 1GB, and you have to have enough free RAM to make garbage collection efficient.

Your app can only get 24 MB for its entire process. Some of it must be kept free for GC, and some of it is taken by other classes, data, and uncollected garbage.

Parsed JSON is essentially a number of nested lists and hashes. Assume that every (boxed) number takes 4-16 bytes, each entry in a hashtable takes another 4-8 bytes for hash + 4 bytes for pointer, plus some overhead due to load factor below 1, strings become UCS2 even if ASCII would be enough, etc. Long JSON with many small objects can easily take 10x the RAM.

Try not to parse the entire file into memory, unless you need the whole of it in RAM for operation. Try streaming parser and writing things into a database.

Community
  • 1
  • 1
9000
  • 39,899
  • 9
  • 66
  • 104
2

One option: android:largeHeap. The official documentation on this is distressing, but AFAICT it allows you 256MB of heap, and it was introduced in 3.0

Community
  • 1
  • 1
Julian Fondren
  • 5,459
  • 17
  • 29
1

Try to use following library to parse your json data

http://code.google.com/p/google-gson/

bindal
  • 1,940
  • 1
  • 20
  • 29