OutOfMemory Error - any ideas?
I'm having problems with a J2ME app I've written.
When I exit the program (destroyapp(true); notifyDestroyed();) in the emulator, and then immediately reload the midlet, the emulator runs marginally slower the second time. If I keep repeating this process, after about 5 reloads I get a java.lang.OutOfMemory error.
I assume the problem is not with the WTK but with my code. Am I not cleaning up properly on closing?
In my code I use RMS - but close my recordstore after using.
I use ByteArrayInput/OutputStreams and DataInput/OutputStreams but close these after using.
I also use a class that implements Runnable where I start a thread but I don't see why this should be a problem.
Any ideas folks? I'm not too clued up on cleaning up system resources to be honest and could do with some pointers.
Cheers,
Rob
P.S I tried using the Memory Monitor to establish what might be going wrong, but it caused a RunTimeException Error (something to do with inconsistent classes). Perhaps these problems are related.
# 1 Re: OutOfMemory Error - any ideas?
Make sure that all threads are properly destroyed and cleanup after themselves. Also, try to isolate the problem by splitting the app to individual segments (simply comment out different code modules each time) and see where the problem lies. It could well be a bug in the implementation of your library, not necessarily something that your code has caused. Another thing: try to help the GC by assigning a null value explicitly to objects once they are no longer used.
Danny at 2007-11-12 0:15:30 >

# 2 Re: OutOfMemory Error - any ideas?
Thanks for getting back to me Danny, I've only just read your post b/c my internet has been up the wall all weekend.
I'll give those suggestions a try, and will read up on terminating my threads correctly.
# 4 Re: OutOfMemory Error - any ideas?
Jeez...I'm sorry its taken so long to get back online. The landlady I'm renting from is having problems with her ISP. Can you imagine no internet for nigh on 3 weeks?? Grrr...
Anyway, I've been trying to set objects to null to aid the GC but it seems to have been having little effect thus far. I have isolated where the error is being thrown, however. I have a set of classes that are initialised using info from data storage, and it is always on these lines that the midlet eventually falls over. I've tried closing my recordstore after access, and setting the db object to null afterwards but still no luck (yet!).
Interestingly, the midlet runs fine on a SE K750i, where I can close and reload as many times as I like without it throwing an exception.
Thanks again for your thoughts. I'll keep you posted...
# 5 Re: OutOfMemory Error - any ideas?
Interesting to note: that setting a few more objects to null after their use, and calling System.gc() on close has increased the number of times the midlet will run before it gives the error.
I guess this has streamlined the program somewhat; there must just still be something thats not freeing up resources each time the midlet closes.
# 6 Re: OutOfMemory Error - any ideas?
OK maybe I've stumbled on the cause. When I 'exit' the app (back to the launch screen), my canvas class that implements Runnable is still running, calling run() over and over.
As you suggested first off, it looks like I am not terminating my threads properly, but I believed that when the midlet closed this would happen automatically; clearly not...
On close and after cleanup I call:
destroyApp(true);
notifyDestroyed();
What is the correct procedure for terminating running threads when you want a midlet to close?
Reading the article http://developers.sun.com/techtopics/mobility/midp/articles/threading2/ on threading, it seems to offer some clues relating to the use of recordstores (which of course I do) and solving potential memory leaks with 'finally', but I'm not sure how this works at the moment...
# 7 Re: OutOfMemory Error - any ideas?
It depends on your design, but the bottomline is that threads don't cleanup after themselves automatically, especially if they hold locked resources that haven't been freed. I guess you should check how many running threads there are at the end, how many of them are still waiting for an event, how many still hold locks to other objects etc. Then, make sure that all the resources are released and that the treads are killed properly. You can simply assign null to all the objects in the finally clause.
Danny at 2007-11-12 0:21:40 >
