OutOfMemory error , use lwuit

311 views Asked by At

I have some button on my form. When I click on every buttons run new form with same buttons. When I am clicking many times, show error OutOfMemory.
I think this is because I create a lot of form objects.
May be can clear stack or use form from stack if form is there?

2

There are 2 answers

0
Lucifer On BEST ANSWER

you need to use the Singleton pattern for your code. In Singleton Pattern, It will create only one object of your Form Class. If the object is null then it will create a new one else , it will return the current one. For this kindly refer to following code.

// Private Constructor

private static myForm thisForm = null;

private myForm()
{
     thisForm = this;
}

// Now to Create Object, you need to create following getInstance Method

public static myForm getInstance()
{
         if ( thisForm == null ) 
         {
                thisForm = new myForm();
         }
         return thisForm;
}

try above logic in your whole code. Your OutOfMemory Problem will 100% get solved.

1
Shai Almog On

You are keeping pointers (references) to old components which causes a memory leak. Make sure never to store components as members of your class unless you clear them later.