如何释放已使用的Connectionrequest的内存

时间:2012-08-08 10:59:43

标签: lwuit

我的应用程序每天会生成一百个新的连接请求并解析json响应。但我已经意识到它在每次请求后都没有释放内存。尽管使用了system.gc(),但它存储了ConnectionRequest实例和我的类实例,用于解析json响应。我想最后它会导致系统内存不足错误。

这里有一些代码:

  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;
  }

我尝试过使用WeakReference,但没有帮助。

 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;
}

1 个答案:

答案 0 :(得分:0)

您不需要调用System.gc(),因为您要为最终变量赋值null,所以上面的代码无法编译。

ConnectionRequests无缝收集,除非你保留对它们的引用,它就像那样简单。 J2ME的WTK 2.5.2有一个内存监视器,允许您查看分配堆栈,对于Codename One,您可以使用NetBeans分析器监视内存分配并将泄漏追溯到它们发起的位置。