简体   繁体   中英

OutOfMemoryError in j2me application

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 .

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.

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();

This problem normally occur when there is short of memory in emulator.

Reasons:

  1. you are using too much images.
  2. you are initializing too much objects.
  3. your device is not as much memory as you need.

Solutions:

  1. you can load your images in decreasing order.
  2. you can remove garbage collection using gc() function.
  3. if some objects are not used assign null to that objects.
  4. do not initialize objects in loop.
  5. do not create same image again and again. just use reference of the same image if you want to use the same image (if you are creating same image in different class this problem may occur)

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM