Saturday, December 23, 2006

An interesting advice for LotusScript developers to avoid memory leaks

In the technote GetProfileDocument Method Appears to Leak Memory; Error "...LookupHandle: Handle Not Allocated", an interesting solution is given for avoiding memory leaks in certain LotusScript operations.

We all have at some time seen that some objects (usually documents in loops) are not properly removed by Notes after they have been processed and we had to call Delete method on the object to free the memory. Well, I think not many of us tried to free even Database object in each loop iteration. But according to the technote, assigning and deleting database object in each loop iteration can make a big difference. It would be interesting to test how this solution affects performance, as I suppose it would take time to reset database object 6000 times.

Excerpt:
The memory usage can be reduced by deleting the object handle to the profile document, but if you delete the object handle to the NotesDatabase object, then the memory usage is greatly reduced.

For example, based on the agent example above, the code would be altered to the following:

Dim session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim i As Integer
-
For i = 1 To 6000
Print Cstr(i)
Set db = new NotesDatabase("server", "database.nsf")
Set doc = db.GetProfileDocument("ProfileDoc", Cstr(i))
Delete doc
Delete db
Next