OutOfMemoryError in j2me application

纵饮孤独 提交于 2019-12-06 12:48:20

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.

Ashish Maheshwari

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

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)

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

Megha

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!