java.lang.OutOfMemoryError
at com.solvoterra.xmlengine.Element.<init>(Element.java:9)
at com.solvoterra.xmlengine.XML_Handler_Main.startElement(XML_Handler_Main.java:71)
at org.apache.harmony.xml.ExpatParser.startElement(ExpatParser.java:146)
at org.apache.harmony.xml.ExpatParser.append(Native Method)
at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:505)
at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:492)
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:308)
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:264)
at com.solvoterra.xmlengine.Project_Man.readXML_File(Project_Man.java:148)
at com.solvoterra.xmlengine.Project_Man.run(Project_Man.java:83)
at java.lang.Thread.run(Thread.java:1102)
I can see from this the error has occurred whilst running a new process which only contains the function to SAX Parse an XML file into memory (RAM) I don't know all the details as this information has been provided by an anonymous user on the market.
Before the function to Parse is called all buffers and memory is cleared of existing data.
Q: Is it possible that the user is trying to Parse a HUGE XML database into memory and the RAM allocated by his phone simply isn't enough to handle his database?
-
Maybe you should consider using the sqlite database and just browse with queries inside it instead of parsing XML ?ykatchou– ykatchou2010年12月29日 14:02:03 +00:00Commented Dec 29, 2010 at 14:02
-
I'm writing an XML Editor you see.Jay Dee– Jay Dee2010年12月29日 14:03:24 +00:00Commented Dec 29, 2010 at 14:03
2 Answers 2
Sax parsing method doesn't load things in memory itself (as opposed to DOM). Only your way of handling events generated by SAX can cause memory overload.
If you allow your users to chose freely an XML source, you have to either:
- build a parsing algorithm which stores in BD or file the result of the parsing one element at a time
- build a parsing algorithm which loads the first X elements of the XML source and if possible allows to fetch following elements when requested (paginate)
- check the length of the XML source before parsing in order to display an error message to the user asking him to load a smaller source
Another reason for this OutOfMemoryError could be a memory leak in your application which is not related to your XML parser. For example, orientation changes, if not handled carefully, can easily cause memory leaks. If your application memory is saturated, the OutOfMemoryError can be triggered by ANY memory allocation, but this single memory allocation may not be responsible for the whole process memory saturation.
3 Comments
Out of memory is easily possible (I mean if XML file placed in RAM). SAX Parser is a stream based parsing approach (in contrary to DOM Parser). So SAX itself can't consume a lot of memory. Best approach would be to save XML to external file/storage and the stream data from file directly to SAX Parser. This kind of approach exactly the way SAX parser should be used.