IBM Domino Java 回收对象的正确方法

IBM Domino Java correct method of recycling objects

 public lotus.domino.Document getParentItemFromServiceOrder() throws NotesException{
    findRegels();
     lotus.domino.Document orderRegelTemp = OrderRegels.getFirstDocument();
     lotus.domino.Document temp1 = null;  
     while(orderRegelTemp != null)
     {

        if(orderRegelTemp.getItemValueString("PARENTLINEITEM").equals("1"))
        {
             if(temp1 != null) temp1.recycle();
             if(OrderRegels != null)OrderRegels.recycle();
             return orderRegelTemp;

        }
        else{
            temp1 = OrderRegels.getNextDocument(orderRegelTemp);  
            orderRegelTemp.recycle();  // recycle the one we're done with 
            orderRegelTemp = temp1; 
        }
     }//end while
     if(orderRegelTemp != null) orderRegelTemp.recycle();  
     if(temp1 != null) temp1.recycle();
     if(OrderRegels != null)OrderRegels.recycle();
     return null;
 }

请问上面的方法中是否需要回收,或者说函数执行完后会自动回收对象吗。。接下来,如果orderRegelTemp返回了对象,我什么时候需要回收?

没有。

显然 OrderRegels 超出了此方法的范围,我猜您正在 findRegels() 中执行某些操作以对其进行初始化。我可能没有那样做,但没关系。但是,您不能 在此方法内回收 OrderRegels,因为您正在 returning orderRegelTemp,它是 OrderRegels 包含的子对象。当您回收 OrderRegles 时,它的所有子项都将被回收。因此,您正在 returning 的对象将在您的方法的调用者尝试访问它时被回收。那将是一件非常糟糕的事情。

这只是第一个问题!

调用 getNextDocument 后,您将 temp1 分配给 orderRegelTemp,然后循环,如果您的 if 条件命中,您要做的第一件事就是回收 temp1。由于 temp1 和 orderRegelTemp 引用同一个 Document 对象,您只是回收了您的方法试图 return 的 orderRegelTemp 的存储!因此,即使您删除了对 OrderRegels.recycle() 的调用,它仍然无法正常工作。

你做对的一件事是在你的 else 子句中调用 orderRegelTemp.recycle() 。这是正确的做法。如果您忽略了这一点,那么您将积累大量 C API 内存,Notes API 正在为这些文档分配内存,并且您很容易 运行 内存不足。由于 Notes API 管理共享内存段的方式,即使在具有大量内存的 64 位系统上也是如此。

关于回收 Domino 会自动做三件事 (AFAIK):

  • 当代理结束时它回收会话
  • 当一个对象被回收时,它会回收所有子对象(-> 当一个代理结束时,它的所有对象都会被回收——一般来说)
  • 当一个线程结束时,它会回收所有由该线程打开的数据库,前提是它们没有被任何其他线程(使用相同的会话)触及

Domino 的自动回收对您帮助不大(实际上它常常使事情变得更糟)。
但是您可以使用回收器自动回收:http://recycler.sourceforge.io