How to recycle Notes objects in XPages and Java
May 28th, 2013Using 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: Java, Javascript, OpenNTF, XPages
May 29th, 2013 at 13:32
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.
May 30th, 2013 at 06:25
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.
June 11th, 2013 at 17:17
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?
June 11th, 2013 at 17:17
And thank you very much!!!
June 11th, 2013 at 19:22
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).
June 13th, 2013 at 15:42
Hi Per,
Many thanks for your answer.
From now on, I'll use this 'one line' recy-nuller:
doc = doc.recycle() || null;
November 21st, 2014 at 11:59
OK, so silly question time 🙂
Why does doc need recycling but tmp doesn't ?
November 21st, 2014 at 12:06
Hi Sean, you should probably recycle tmp when the loop ends. Since you reuse tmp inside the loop, I am not sure what would happen if you added a recycle call. Give it a try and let us know 🙂
November 21st, 2014 at 14:10
So why not just recycle doc at the end ?
is it because in the IBM example they set
Document tmp = null; before the loop ?
would post link but anti spam engine doesn't like it