25. Notes About Garbage Collection In LotusScript

Garbage collection is done with three characteristics in Lotusscript.
a) It’s done after the execution of EACH line of code.
b) It reclaims the memory of an object when on local variable references it directly, even when another referenced object links to it.
c) When it reclaims an object, it reclaims all the objects contained by the object. Notes does not allow free-floating objects that exist outside their container, or "owner" object. Documents, for example, must belong to a database. Items must belong to a document, and ACLEntries must belong to an ACL object.
Point a) ensures the efficient memory usage when a sub involves the iteration of a large number of Notes objects e.g. documents.
set view = db.GetView("$all") 
set doc = view.GetFirstDocument() 
while not (doc is nothing) 
      ' do something... 
      set doc = view.GetNextDocument(doc) 
wend

Point b) and c) are harmful when they work together.

dim s as new NotesSession 
dim entry as NotesEntry 
set entry = s.CurrentDatabase.ACL.GetFirstEntry
The entry object will be reclaimed right after the assignment statement is executed as the ACL object is not stored in any variable and thus destructed, which causes the child object ACLEntry to be reclaimed. The issue in this case can be fixed by storing the ACL object in a variable.
Another case is as below:
Sub Initialize
	Dim s As New NotesSession
	Dim view As NotesView
	Set view=TestObj()
	Print view.Name
End Sub
Function TestObj As NotesView
	Dim s As New NotesSession
	Dim db As NotesDatabase
	Set db=s.Getdatabase(s.Currentdatabase.Server, "log.nsf")
	Set TestObj=db.Getview("UsageByUser")
End function
The View object returned by the function TestObj is reclaimed before it can be used by the caller because the parent database log.nsf is reclaimed after the function ends. If the view is from the current database, it will keep alive, even if the current database object is not initialized in the caller. The current session and database objects keep alive until the whole program e.g. agent exits. To avoid the failure above, a global variable can be declared to hold the exterior database object.

Sub Initialize
	Dim s As New NotesSession
	‘the following two lines are optional
	Dim db As NotesDatabase
	Set db=s.Currentdatabase
	Dim view As NotesView
	Set view=TestObj()
	Print view.Name
End Sub
Function TestObj As NotesView
	Dim s As New NotesSession
	Set TestObj=s.Currentdatabase.Getview("All")
End function



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值