How to recycle Notes objects in XPages and Java

May 28th, 2013

Using Notes objects in XPages and in Java require that you recycle those objects in order to avoid memory leaks and backend out of memory issues. Here I will summarize how to do proper recycling in XPages for server-side javascript, Java and Java using the new OpenNTF project org.openntf.domino.

Server-side JavaScript

With server-side JavaScript you have to use the recycle method available on Notes objects and you have to "nullify" your server-side JavaScript variables. Thank you, Tony McGuckin, for clarifying this.

Here's a very simple example that assumes that the variable doc has been initialized elsewhere:

doc.recycle();
doc = null;

Java

In Java you have to use the recycle method available on Notes objects. So for looping through a document collection you have to do this:

try {
	Database dbCurrent = ExtLibUtil.getCurrentDatabase();
	DocumentCollection dc = db.getAllDocuments();
	Document doc = dc.getFirstDocument();
	While (doc != null) {
		// process doc
		tmp = dc.getNextDocument(doc);
		doc.recycle();
		doc = tmp;
	}
	dc.recycle();
} catch (NotesException e) {
	e.printStackTrace();
}

org.openntf.domino

With org.openntf.domino you do nothing with regards to recycling! So when looping through a document collection you can concentrate on your business logic:

Database db = Factory.getSession().getCurrentDatabase();
for (Document doc : db.getAllDocuments())
	// process doc
}

Tags: , , ,

6 Responses to “How to recycle Notes objects in XPages and Java”

  1. Simon O'Doherty Says:

    Regarding your first example. Do your dc.recycle() in a finally block. In the loop dies you can be left with memory floating around. Particularly annoying leaks to find.

  2. Declan Lynch Says:

    If you use a finally block then the variable must be declared above the try block. Just a simple DocumentCollection dc = null; will do the trick then you can set it to the actual document collection inside the try block.

  3. Txemanu Says:

    So, in a ssjs loop, do you have to do something like this?
    -----------------------------------------------------------------------------------
    var view:NotesView = database.getView("myView"),
    doc:NotesDocument = view.getFirstDocument(),
    temp:NotesDocument;

    while (doc) {
    temp = view.getNextDocument(doc);

    // .....

    doc.recycle();
    doc = null; // << also nullify here?
    doc = temp;
    }

    doc = temp = null; // << or just nullify here

    view.recycle();
    view = null; // << I suppose always here.
    -----------------------------------------------------------------------------------
    And... is there any site where I can read that 'nullify' discussion?

  4. Txemanu Says:

    And thank you very much!!!

  5. Per Henrik Lausten Says:

    Hi Txemanu. Inside the loop you do not have to nullify as you assign the variable to temp right after.
    You need to recycle and nullify the doc variable outside the loop.

    Tony clarified the issue with nullifying objects in a call. I have not seen it stated elsewhere (for XPages and SSJS).

  6. Txemanu Says:

    Hi Per,

    Many thanks for your answer.
    From now on, I'll use this 'one line' recy-nuller:

    doc = doc.recycle() || null;