Reputation: 7966
I am getting OutOfMemoryException
in a J2ME Application.
How can I find what is causing this error? And how to prevent getting this error ?
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 OutOfMemoryException
.
Upvotes: 2
Views: 3463
Reputation: 11
This problem normally occur when there is short of memory in emulator.
Reasons:
Solutions:
gc()
function.Upvotes: 1
Reputation: 11
Your OutOfMemoryException
while image display on mobile is due to lack of memory in heap this can be done by. Running Garbage collector as System.gc();
but unfortunately it does not work in J2ME.
So, we can use here
Runtime.getRuntime().gc();
instead of
System.gc();
Upvotes: 1
Reputation: 1581
OutOfMemoryException
comes in j2me because of the variable is not free its memory after its usage is complete. we can explicitly free the memory for the variable.
After the task is complete free the memory for that variable. Try to not load all images at once do lazy loading for this. Image take large space compare to other variable. so use low quality image and also not use customized font in the application use system font.
Upvotes: 0
Reputation: 841
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
Upvotes: 0
Reputation: 1265
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.
Upvotes: 0
Reputation: 32189
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.
Upvotes: 2