views:

815

answers:

4

I'm getting OutOfMemoryError in a j2me application.

How can I find what is causing this error? And how to prevent getting this error?

EDIT: I make a http request. While the request isn't completed the screen shows a loading image (like a browser shows when a page is loading). This was done by creating an Image and repainting the screen.

create image 1 -> repaint -> create image 2-> repaint-> create image 3 -> repaint -> create image 1-> repaint -> ....

I noticed (using wtk memory monitor) that this was consuming too much memory that wasn't garbage collected.

Then I tried to create a class that is kind of pool of images. This class creates all the images and then show them.

create image 1 -> create image 2-> create image 3 -> repaint -> repaint -> repaint -> repaint -> repaint -> ...

This second scenario doesn't seem to consume as much as memory than the first one. (using wtk memory monitor).

However, I think (not sure if is this) those both approaches are contributing to cause this OutOfMemoryError.

+1  A: 

The cause of the error is a lack of memory. Sorry to state the obvious, but you asked :-)

Some source code would be required in order to diagnose the exact problem.

You should also look for parts of your code that are either making recursive method calls or allocating memory inside a loop. Recursive calls would normally generate a StackOverflowException, but it's worth a look. Allocating memory inside a loop can quickly lead to an OutOfMemoryError though.

William Brendel
I know is lack of memory. But the application is simple enough and shouldn't have this issue.
Daniel Moura
That's why I asked for source code. I said it is a "lack of memory" tongue in cheek.
William Brendel
Added some more information in the question.
Daniel Moura
A: 

Depending on the constraints of the device, creating and keeping 3 full-screen images might be a problem.

Are your three 'loading' images drastically different? Or are they largely the same image, with only a small portion different from image to image (for example, all are various images of a 'spinning wheel' in the middle of a large white field)?

If you can get away with having Image 1 be the full image you're displaying, and image 2 and 3 be small images that could be drawn on top of Image 1, you could save a lot of memory that way.

That is: Create images 1-3 at the beginning. Then, on repaint(), always draw image 1, and optionally image 2 or image 3, depending on the step in the animation.

Matt Poush
All images are small (16x16, ~460bytes) and I'm passing the position to repaint: repaint(100, 73, 16, 16). And I'm using 8 images, not 3.
Daniel Moura
How large is the content being returned in your http request? Could that be what's causing your memory errors?If you're sure that the memory problem is the image creating/keeping/painting, you might try using the Sprite class. It sounds like it's similar to the class you created for scenario #2, and is likely optimized for the tight constraints of mobile devices.
Matt Poush
The http response is small. I'm sure that the image is not the *only* problem. Hate maintaining someone else's code! I'll check this Sprite class.
Daniel Moura
A: 

What is triggering the repainting? Do you have a TimerTask that is doing it? Loading those images should be fine, I've loaded many more images than that without an issue.

jjb
A: 

Create the images only once and reuse them wherever you want and make the their reference to null as soon as the requirement is over. This will make them to be garbage collected.

Don't create any unwanted variables or objects(especially image objects).

You can initiate the garbage collection explicitly by calling System.gc(). But calling this frequently may affect the performance

Jinesh