views:

98

answers:

3

Hello,

I recently created a program that gets medi-large amounts of xml data and converts it into arrays of Strings, then displays the data.

The program works great, but it freezes when it is making the arrays (for around 16 seconds depending on the size).

Is there any way I can optimize my program (Alternatives to string arrays etc.)

+2  A: 

3 optimizations that should help:

Threading

If the program freezes it most likely means that you're not using a separate thread to process the large XML file. This means that your app has to wait until this task finishes to respond again.

Instead, create a new thread to process the XML and notify the main thread via a Handler when it's done, or use AsyncTask. This is explained in more detail here.

Data storage

Additionally, a local SQLite database might be more appropriate to store large amounts of data, specially if you don't have to show it all at once. This can be achieved with cursors that are provided by the platform.

Configuration changes

Finally, make sure that your data doesn't have to be reconstructed when a configuration change occurs (such as an orientation change). A persistent SQLite database can help with that, and also these methods.

hgpc
And welcome to stackoverflow, Nick.
hgpc
Thanks but I don't have control over the data being fed to me :s (the website isn't mine)
Nick
Okay, I've tried this threading thing/Async tasks but the program crashes when the ".execute" method is called. Are there any rules/reccomendations on what I should/shouldn't have happening inside of the Async task class? (sorry, I'm new to this :|)
Nick
@Nick I meant a local SQLite database. All the optimizations I mentioned are under your control. Concerning the AsyncTask crash, it might be best if you post your code in a separate question.
hgpc
Ah, ok, thanks :)
Nick
+1  A: 

You can use SAX to process the stream of XML, rather than trying to parse the whole file and generating a DOM in memory.

Tim McNamara
+1: This can really speed things up. I've been surprised by how much memory and time it takes to do a DOM parse of moderately large input.
Jim Ferrans
I already use SAX. I wrote A whole API for the website xD
Nick
A: 

If you find that you really are using too much memory, and you have a reason to keep the string in memory rather than caching them on disk, there are certainly ways you can reduce the memory requirements. It's a sad fact that Java strings use a lot of space. They require two objects (the string itself and an underlying char array) and use two bytes per char. If your data is mostly 7-bit ASCII, you may be better of leaving it as a UTF-8 encoded byte stream, using 1 byte per character in the typical case.

A very effective scheme is to maintain an array of 32k byte buffers, and append the UTF-8 representation of each new string onto the first empty space in one of those arrays. Your reference to the string becomes a simple integer: PTR = (buffer index * 32k) + (buffer offset). "PTR/32k" yields the index of the desired byte buffer, and "PTR % 32k" yields the location within the buffer. Use either an initial length byte or a null terminator to keep track of how long the string is. When you need to access one of the strings, don't allocate a new String object: unpack it into a mutable StringBuilder or work directly with the UTF-8 byte representation.

The above approach is obviously a lot more work, but can save you between a factor of 2 and 6 in memory usage (depending on the length of your strings). However, you should beware of premature optimization. If your problem is with the processing time to parse your input, or is somewhere else in your program, you could find that you've done a lot of work to fix something that isn't your bottleneck and thus get no improvement at all.

beekeeper