0

My application makes hundred new Connection requests a day and parses json responce. But I've realized that it doesn't free the memory after each request.Though using system.gc() it stores the ConnectionRequest instance and instance of my class for parsing json responce. I guess at the end it causes system out of memory error.

here is some code:

  protected boolean onMainFormCommand54(){
         //LevakandRestService similar to GoogleRestService
        final LevakandRestService r =new LevakandRestService();
        r.RequestUBalance(balance);        
        final Progress p = new Progress(Globals.Loading, r);
        p.showPacked(BorderLayout.CENTER, false);
        p.setDisposeOnCompletion(true);
        r.addResponseListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {                            
                    p.dispose();                                      
                    Dialog.show(Globals.Info, "Balance:" +balance.getBalance(),Globals.OK, "");
            }
        });
       NetworkManager.getInstance().addToQueueAndWait(r);

       System.gc(); 
      return val;
  }

I've tried using WeakReference but it doesn't helped.

 protected boolean onMainFormCommand54() {//Balance of user

    boolean val = super.onMainFormCommand54();       
    Balance balance1 = new Balance();
    WeakReference wrb = new WeakReference(balance1);       
    final Balance balance =(Balance)wrb.get();
        LevakandRestService r1 = new LevakandRestService();            
         WeakReference wr = new WeakReference(r1);             
        final LevakandRestService r =(LevakandRestService)wr.get();
        r.RequestUBalance(balance);        
        final Progress p = new Progress(Globals.Loading, r);
        p.showPacked(BorderLayout.CENTER, false);
        p.setDisposeOnCompletion(true);
        r.addResponseListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {                              
                    p.dispose();                                      
                    Dialog.show(Globals.Info, "Balance:" +balance.getBalance(),Globals.OK, "");
            }
        });
        NetworkManager.getInstance().addToQueueAndWait(r);
        r1=null;                    
    balance1 = null;    
    System.gc();
    return val;
}
AzizD
  • 103
  • 10

1 Answers1

0

You don't need to call System.gc() and the code above doesn't compile since you are assigning null to a final variable.

ConnectionRequests get collected seamlessly unless you keep a reference to them, its as simple as that. J2ME's WTK 2.5.2 has a memory monitor which allows you to see the allocation stack and for Codename One you can use the NetBeans profiler to monitor memory allocations and trace leaks back to the place where they originated.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • sorry for first part of code it's a bug left after publishing here but I just wanted show that I don't keep any reference to ConnectionRequest after use. and after call System.gc() it's still remains in the memory. I've checked it through WTK 2.5.2 memory monitor. It's the whole code I use the question is where that reference could be holded? I've also checked the Recipe book sample with GoogleRestService and realized that it remains in the memory too. – AzizD Aug 10 '12 at 05:36
  • Using a weak reference to something where you have a hard reference to it right after won't make much of a difference. The pointer to r still exists so setting other pointers to null or placing them in a weak reference doesn't make sense. For this you need to use the Java SE profiler/memory monitor which is far superior to the WTK memory monitor and works with Codename One. – Shai Almog Aug 15 '12 at 15:44