Search code examples
java-melwuitout-of-memorylwuit-formlwuit-button

OutOfMemory error , use lwuit


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?


Solution

  • 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.