outOfMemory exception on device with 2mb memory
Hi all
I have experienced the following problem.
The application I have developed works fine on a sonyerricson w800i but gives outofmemory on a nokia 6131
The se800 reports 1048572 bytes available memory (1mb)
The nokia 6131 reports 2097152 bytes available memory (2mb)
For debugging purposed I do a System.gc() and report free memory, on average the se800 has 77% free whereas the nokia starts with 55% free and it degrades quickly.
Its the same application and code so does this mean the nokia has a problem? It is my code that is at fault?
When I first encountered outOfMemory exception I followed some guidelines removing any inner classes dereferencing object by obj = null and making sure I close streams etc, this helped with general memory consumption. I tested it with the emulator limiting memory to 400k and I have an average of 20% free.
I suspected the screensize to play a role as I paint the canvas manually but on the emulator I use the defaultcolorphone which has a larger screen than the nokia.
Where could the problem lie?
GX
Re: outOfMemory exception on device with 2mb memory
try to optimize ur code. use as minimum forms as u can use. what are u trying to do in your code. if u could explain we might be able to help u a lot better.
Re: outOfMemory exception on device with 2mb memory
Hi nicenouman
Thanks for the reply, The application is a simple collection of texts categorised into folders. there is a main menu a tree based browse form and a view text form.
obstuficating the code makes little difference
Like I said on once device when the program starts it consumes 300k and on the other 1000k.
what puzzles me is how can one implementation of the same code be so bloated?
on the emulator I can run the code with 400k memory limit and its fine. what gives?
GX
Re: outOfMemory exception on device with 2mb memory
i have experience problems similar to what u have described. problem is by my understand once u create a form and after using it when u go to another display form the previous form remains in the memory with the assumption that user might want to come back to the first form. so after few opening and closing of form what happens is u get out of memory exception this was my own experience and so far i havent been able to find solution to this issue.
anyway about one implementation using 400k and other 1000k. how do u know that its only ur midlet which is using this memory. there might be a possiblity that another midlet might be running in background sharing the memory with u. which device are u using and how are u calculating the amount of memory ur midlet is using can please explain a little?
Re: outOfMemory exception on device with 2mb memory
Hi Nauman and GX
Just for a suggestion you both could try nulling the form,running a System.gc() and recreating the form if opening and closing is causing u the issue.
Re: outOfMemory exception on device with 2mb memory
Hi adilb and Nauman
Thanks for your replies..
Here is how I determine the amount of free space:
[CODE]
public static void debugOutput(String msg)
{
if(DEBUG_SHOW_OUTPUT)
{
String fm = "Free mem: " + getFreeMem() + " - ";
if(DEBUG_SHOW_SPEED)
fm += debugMsPassedSinceLastOutput();
fm += msg;
System.out.println(fm);
if(_currentScreen != null)
_currentScreen.debugToFooter(fm);
}
}
public static String getFreeMem()
{
System.gc();
long tot = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().freeMemory();
int per = (int)(((float)free / (float)tot) * 100);
String s = gx.Utils.padString(String.valueOf(free), -8, " ") + " " + gx.Utils.padString(String.valueOf(per), -3, " ") + "%" + " / " + String.valueOf(tot);
return s;
}
[/CODE]
I have tried string buffers to build the strings in the above example it made little difference so I removed them from this example for readability sake.
From my understanding what you are saying is that is that the platform is caching the form in memory, this cant be that drastic as the memory consumption I quoted was soon after the app starts.
I am certain that no other apps are running because I restarted the phone, I have the phone now for debugging purposes and am running tests on both phones simultaneously, also the nokia shows how much memory is free for applications in the memory status screen.
As mentioned originally the two devices are sonyerricson w800i and nokia 6131, the nokia is the problematic device.
the flow of the application is as follows:
public class ATRMidlet extends javax.microedition.midlet.MIDlet
-> public void startApp()
{
_navigator = new gx.Navigator(getDisplay(), this);
}
the navigator class keeps a reference to the midilet (this) so it can call exitMIDlet()
the navigator then loads the data from the recordstore
then the mainmenu form is initialised
public class MainMenuForm extends gx.Forms.BaseForm
public abstract class BaseForm implements gx.Forms.CustomForm.CommandListener
base form does the command handling and creates an instance of
gx.Forms.CustomForm() which does the handles items, it also extends customCanvas which does the painting
public final class CustomForm extends gx.Forms.CustomCanvas
-> public class CustomCanvas extends javax.microedition.lcdui.Canvas
the debugoutput is called at various points namely when painting here is some output from the emulator:
Free mem: 301928 73% / 409600 - Navigator started init
Free mem: 247224 60% / 409600 - CustomCanvas paint started
Free mem: 247032 60% / 409600 - CustomForm paintMain started
Free mem: 247032 60% / 409600 - CustomForm paintMain ended
Free mem: 247172 60% / 409600 - CustomCanvas paint ended
Free mem: 247172 60% / 409600 - CustomCanvas paint started
Free mem: 199272 48% / 409600 - CustomCanvas paint ended
Free mem: 199796 48% / 409600 - Navigator pre init icons
Free mem: 197412 48% / 409600 - Navigator pre init object handler
Free mem: 185064 45% / 409600 - ObjectHandler init started
Free mem: 147684 36% / 409600 - Navigator post init object handler
Free mem: 129896 31% / 409600 - BaseForm init started title: Basic menu
Free mem: 129820 31% / 409600 - CustomCanvas init started
Free mem: 129756 31% / 409600 - CustomCanvas init ended
Free mem: 129756 31% / 409600 - CustomForm init started
Free mem: 129740 31% / 409600 - CustomForm init ended
Free mem: 129452 31% / 409600 - BaseForm init ended
Free mem: 129428 31% / 409600 - MainMenuForm setMenuItems started
Free mem: 129348 31% / 409600 - MainMenuForm pre init menu cat items
Free mem: 124888 30% / 409600 - MainMenuForm post init menu cat items
Free mem: 124580 30% / 409600 - MainMenuForm setMenuItems ended
Free mem: 125104 30% / 409600 - Navigator ended init
Free mem: 123428 30% / 409600 - CustomCanvas paint started
Free mem: 122040 29% / 409600 - CustomForm paintMain started
Free mem: 122568 29% / 409600 - CustomForm paintMain ended
Free mem: 122692 29% / 409600 - CustomCanvas paint ended
at this point the main menu is displayed and waiting for user input.
as you can see there is 29% free memory 122692 of 409600 bytes
so at this point only one instance of canvas is created and at the same point on the nokia with 2mb RAM it reports 1170432 of 2097152 bytes 55%
when the next form is loaded it reports less an less free memory whereas the sonyerricson reports between 80% and 70% free
so System.gc() is being called before I check for free space, and I am currently finding all the points where I can set objects = null and doing so has had little improvement.
my apologies for the long post
GX
Re: outOfMemory exception on device with 2mb memory
"Just for a suggestion you both could try nulling the form,running a System.gc() and recreating the form if opening and closing is causing u the issue."
adilb i did try this nullifying every form and calling System.gc() it did not work. what i havent been able to understand why it allows the form to show couple of times and then suddenly i get out of memory exception.
gx problem with Runtime.getRuntime().totalMemory() and Runtime.getRuntime().freeMemory(); is that it will give u a value of the memory at that time u made the query. it is possible midlet may not be using the entire memory at that time. or it may be using some extra memory to perform certain operation. so u can never be sure about this. also free memory gives the amount of heap size available which is usually 1 MB but most mobile also increase the heap size as needed so again it is not a completely reliable way of dealing with it.
try to change ur device and see if that works for u or not.