`
从此醉
  • 浏览: 1034550 次
  • 性别: Icon_minigender_1
  • 来自: US
社区版块
存档分类
最新评论

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



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics