Code: AQXMLParser

Got a big (like, 22 MB) XML file to parse on the iPhone? Want to reduce the memory usage from NSXMLParser’s huge (like, 123 MB) virtual memory usage by several orders of magnitude down to almost nothing (like, 68 kb)?

Yeah, we thought you did.

The magic trick is to use “Alan Quatermain”s AQXMLMemoryParser:

Here’s the output. I think it shows that AQXMLParser is going to be *lovely* for the iPhone. Mwa-ha-haa.

Testing NSXMLParser from URL...
Parsed 200000 numbers
Peak VM usage: 128991232 bytes

Testing NSXMLParser with mapped data...
Parsed 200000 numbers
Peak VM usage: 130035712 bytes

Testing AQXMLParser...
Parsed 200000 numbers
Peak VM usage: 69632 bytes

That’s right, the NSXMLParser variant (calling xmlParseChunk() with all data at once) uses 123-124MB, while AQXMLParser (calling xmlParseChunk() with 1KB at a time, not loading all data anywhere) gets 68KB.

In short: AQXMLParser == T3h w1n!

Indeed. Source is in AQToolKit on github which is just full of all sorts of other useful goodies as well.

h/t: iPhone Development!


Alex | June 9, 2009

Leave a Reply