There are always new objects being created. The Graphics object passed to paint(), for example. Any kind of String manipulation. Any temporary objects created by API methods that you call. The local variable frame created every time a method is called.
Arrays are objects. This line:
Code:
int[][] a = new int[3][3];
creates four new objects: one int[][] and three int[]. Objects typically consume 16 bytes, plus the space they need rounded up to a multiple of four. That 3x3 array of ints, that you might think needs ((3 * 3) * 4) = 36 bytes (4 bytes for each int) actually consumes 16 bytes of overhead + 12 bytes of data (4 bytes per int or per object reference) for each of four arrays (112 bytes).
On the subject of arrays, beware of code like:
Code:
int[] a = new int[] { 1, 2, 3 };
This compiles exactly like:
Code:
int[] a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
So it can generate much more code than you expect (if you're from a C or C++ background). It can also mean you're executing a lot of code (and generating garbage) for what you thought was a simple pointer copy. Might not be a problem if this code is in a class-initializer, but in a method, or especially in a loop...
A String is an object (16 bytes) containing an int (4 bytes) and a reference (4 bytes) to a char[] (which is an object (16 bytes) containing two bytes per character). Total, 40 bytes + 2 bytes per character (rounded up to a multiple of four). A large number of small strings can take a lot more space than you think.
Code:
if (someString.trim().toLowerCase().equals("fish")) {
This line creates two String objects (one the result returned from trim() and the other returned from toLowerCase()). If the string was four characters long, then that's 96 bytes gone. (Plus any space used by those methods.)
So, back to your free-memory log... there are lots of fairly small drops, say 1000 bytes, and that is what I would expect to see. However, you have some much larger drops (as much as 400k), so something else must be going on there.
Be wary of worrying too much about memory allocation in the emulator. Worry only if you're seeing the same behaviour on the device. It is easy to spend a lot of time chasing down bugs or other issues that turn out only to happen on the emulator!! In this case, by "behaviour", I don't mean "memory drops and drops, then bounces back up". You *will* see that. I mean, if it stutters on the device. You probably won't... the emulator's VM has been tuned for debugging, whereas the VM on the device will have been tuned much more for performance.
As Raj mentioned in an earlier post, there is a memory monitor in the wireless toolkit that will tell you things like how many objects of what types you have. If you want to investigate further, try using that tool to see what's going on.
Hope that helps some.
Graham.