Garbage collection in Lotus Notes-Lotus script

Source: Internet
Author: User
Tags stack pop
Java makes the concept of garbage collection not only familiar to environmental protection personnel, but also a required course for programmers. Let's take a look at its definition: Garbage collection is an automatic memory management form. The objects in the collector monitoring program are reclaimed when they are no longer needed. In contrast, SyntaxHighlighter. all (); Java makes the concept of garbage collection no longer familiar to environmental protection, but also a required course for programmers. Let's take a look at its definition: Garbage collection is an automatic memory management form. The objects in the collector monitoring program are reclaimed when they are no longer needed. In contrast, the programmer manually specifies when to destroy the object. In fact, garbage collection is natural. Variables of various basic data types declared in the function are created in the stack. The memory occupied when the function is exited is automatically reclaimed because the stack pop-up occurs. It does not require the attention of programmers or special processing of computers. However, the stack, as a mechanism for implementing function calls, is only suitable for storing small data. If it is a complex data type (large size), such as an object, it will be created in other specialized memory areas, in Java is heap ), only the pointer that references this object is saved in the stack. When the function exits, the local variables in the function become invalid, and the referenced objects remain in the memory. Languages like C ++ require programmers to manually destroy objects. Practice has proved that this work is cumbersome and error-prone. People don't want to do it, they just think of making computers work for themselves, and the tedious and error-prone work is exactly the machine's expertise-meticulous and tireless. So the garbage collection technology was born. Returning to Lotus Notes, garbage collection has been playing a role, but it is so quietly done that most people have not noticed it. In Lotus script, we use the Notes object and create it. The memory occupied after they exit the scope is automatically recycled. This is actually quite natural, because the COM object used by the VBA that Lotus script originated uses the reference count-based garbage collection. The following uses a few examples to test the knowledge of the garbage collection feature in Lotus script. Example 1 [vb] Function GetConfigDoc (key As String) As NotesDocument Dim s As New NotesSession 'get the db ining the config documents Dim server As String, path As String server = "SERVER" path = "abc. nsf "Dim db As NotesDatabase Set db = s. getDatabase (server, path) Set GetConfigDoc = db. getview ("vwConfig "). getdocumentbykey (key, True) End Function Sub Initialize Dim doc As NotesDocument Set doc = GetConfigDoc ("No") Print doc. values (0) End Sub Example 2 [vb] Dim entry As NotesViewEntry Set entry = nav. getchild (personEntry) 'nav is a NotesViewNavigator Dim I As Integer For I = 1 To entry. A Siblingcount-1 entry. document. type = "Arrival" Call entry. document. save (True, False) Set entry = nav. getnextsibling (entry) Next zhujun visually tests the running results of the above two sections of code? The actual result is that the Code in Example 1 reports the error Object variablenot set. The code in example 2 does not work after it is run. If you encounter such a situation at work, you may break your head and finally have to change the way you write. The syntax does not have any errors. what's even more annoying is that running on a line-by-line basis does not show any exceptions. The reason is the garbage collection of Lotus script. Let's take a look at the code: [vb] Set view = db. getView ("$ all") Set doc = view. getFirstDocument () While Not (doc Is Nothing) 'Do something... set doc = view. getNextDocument (doc) Wend: This is a clip that we have encountered countless times and looks simple. Even if there are 50,000 documents, the program can run very stably and occupy no more resources than 50 documents. Let's take a closer look. In the while LOOP, doc variables are constantly directed to new document objects. Where are the old document objects? This is not the case for automated garbage collection applications. The LotusScript interpreter automatically destroys these objects, which is completely transparent to programmers. Everything is fine, right? Why is the result of Example 1 and Example 2 abnormal? This is because the garbage collection of Lotus script has the following characteristics: 1. Each line of code is executed after it is run to check whether there are no variables referenced by objects. When the function exits, all objects pointed to by local variables are recycled. 2. An object will be recycled as long as there is no local variable pointing directly to it, even if other objects are associated with it through properties. 3. When an object is recycled, all other objects contained in it will be recycled. Contains indicates that an object is obtained by the methods or attributes of another object. For example, an object obtains a view from a database object, a document from the view, and a domain object from the document. Because of the "Tree" Relationship of the Notes object, when a database object is destroyed, its views, documents, and other objects will be recycled. When a NotesSession is recycled, all objects involved in this session will also be destroyed. It can be seen that this mechanism is simple and effective, but it is too much, especially the first and third points play a role at the same time. Now we can explain the actual running results of Example 1 and Example 2. In example 1, The GetConfigDoc function returns a document object, but the database objects containing this document are recycled immediately. According to the third feature above, this document object is also recycled, therefore, document variables cannot be accessed. In Example 2, after the modified line of the Document Object pointed to by NotesViewEntry is executed, garbage collection runs. Because no variable points to it, the document object will be destroyed before the next line is called and saved. When you call the Save method, the Document Object is retrieved from the NotesViewEntry again. In actual programming, we may accidentally fall into this overly powerful garbage collection trap. Fortunately, after learning about its features, it is easy to avoid it. You only need to slightly change the code. For example 1, we can directly use the function to return field values such as strings and numbers in the configuration document, or create a database variable pointing to the configuration database in the function that calls it. In Example 2, we cannot save the steps to declare a variable in the document. As a result, the document object will not be recycled because of the variable reference: [vb] Dim doc As NotesDocument Dim entry As NotesViewEntry 'a record entry Set entry = nav. getchild (personEntry) 'The first record Dim I As Integer www.2cto. comFor I = 1 To entry. siblingcount-1 Set doc = entry. document doc. type = "Arrival" Call doc. save (True, False) Set entry = nav. getnextsibling (entry) Next
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.