Search code examples
xpagesxpages-ssjs

Do I have to use recycle Notes objects in recurrence functions?


I have a recursive function and it uses the Notes objects. Please tell whether I should call recycle method for these objects, or I can leave that to garbage collector?

function getAllUsersInDepartmentAndSub (nd: NotesDocument, arrData: Array) {
  var strSubPrefix = strTaskLibName + "getAllUsersInDepartmentAndSub/";
  try {
    var ndcResp: NotesDocumentCollection;
    var ndResp: NotesDocument;
    var ndNext: NotesDocument;
    var strTemp: string;

    if (nd) {
        ndcResp = nd.getResponses();
        if (ndcResp) {
            if (ndcResp.getCount() > 0) {
                ndResp = ndcResp.getFirstDocument();
                while (ndResp) {
                    strTemp = @LowerCase(ndResp.getItemValueString("form")); 
                    if (strTemp == "department") {
                        getAllUsersInDepartmentAndSub (ndResp, arrData);
                    } else if (strTemp == "person") {
                        strTemp = ndResp.getItemValueString("fullname");
                        if (@Member(strTemp, arrData) <= 0) {
                            arrData.push(strTemp);                          
                        }
                    }
                    ndNext = ndcResp.getNextDocument(ndResp);
                    ndResp.recycle();
                    ndResp = ndNext;
                }
            }
            ndcResp.recycle();
        }
    }
  } catch(e) {
    writeInfo(strSubPrefix, e, true, true);
  }
}

Solution

  • In short: Recycle Recycle, Recycle!

    You have to recycle all Domino objects as soon as they are not needed anymore. And you have to be carefull: If you miss an instance it is horrible to find a recycling problem in your code.

    In your code above, your NotesDocumentCollection ndcResp is not recycled when an error is thrown, and your server runs into allocation problems sooner or later.

    You should use the finally statement and recycle all objects used in your function, so they will be cleaned up even if something in your method goes wrong.