The importance of using the recycle () method to Java objects

Source: Internet
Author: User


When you want to directly access the Lotus Notes objects information of the same server locally, you can use the Java agent and small service. Program (Servlet), a small application (applet) or an independent application.

The applet [applet] simulates the LotusScript object model to make it easier for developers to use these tools. For example, you can use LotusScript or Java class to obtain a database handle and a session that contains database information ). Once a session is created, you can reference the database through this session ).

However, to achieve optimal system performance, it is very important to recycle created objects. For LotusScript objects or other Java objects. When is the best time to call method recycle?

Answer:

Use LotusScript to create objects

When using LotusScript to create an object, you actually create two objects: one in Lotus script and the other in the Notes background (C ++ code. Lotus script only points to the C ++ object, and the row that actually implements the class is C ++. When the agent or event of LotusScript ends, these two objects are automatically revoked. If the object is not eliminated, they will continue to be stored in the memory, resulting in Memory leakage.

Create an object using Java
If you use Java to create an object, the same is true. A Java object is a pointer to an object created using C ++. This is in the pure Java Code Is very common, by setting some variables to null, so that garbage collection can be executed immediately. This mechanism is called active garbage collection.

However, when Java uses the notes object, the related variables are set to null to mark the need for garbage collection, and the corresponding C ++ object is not processed. Garbage collection will only act on Java objects without affecting C ++ objects.

Why recycle?
Recycle () recycles the C ++ object and sets the Java object to the garbage collection state. If you only mark the object as null, you will still encounter memory problems. When you use objects in the proxy, all objects (whether Java or C ++) will be recycled at the end of the contemporary rationale. When using Java server programs (servlets),. jsp, or independent applications, the recycle () method must be used because Domino itself cannot clear these background objects.

On the contrary, if an object is set to recyclable, it will not be set to null ). Theoretically, you can still reference this object without causing a catastrophic error, but throw an exception ). This is important when multiple references call the same object, for example, "DB = mydb. nsf" and "DB = yourdb. nsf. This is very confusing and the results are not reliable.

It is important to understand what objects will be created, how they will be set, and when they will be used. An object is a hierarchical structure. For example, if a parent document object is recycled, all its sub-objects are recycled accordingly. Because the session object is at the top of the entire Object layer, when a session object is recycled) all other objects in the object.

Query object:
Go to the design panel in Domino designer and click the code area to query "recycle" or any objects you want to query. And set the query range to the current object or all objects.

If you haven't found any recycle () method, you can find a developer and add some code after all the objects are used to recycle them. Note that when a class is instantiated using new in a loop, a new object will be created every time a loop is executed, unless you recycle this object in the loop body, otherwise, the object will remain in the memory.

Note:
The following code is a script example that describes how to solve this problem. You can use this script, but you have to take risks on your own. For execution purposes, the script must specify the following. Product Support cannot customize the specific environment or application of this script.
Example of recycling with recycle () and active garbage collection:

Import Lotus. Domino .*;
Public class javaagent extends agentbase {
Public void notesmain (){
Try {
Session session = getsession ();
Agentcontext = session. getagentcontext ();
Database DB = agentcontext. getcurrentdatabase ();
View v = dB. getview ("someview ");
// Turn off auto-update so that if we make a change to a document // and resave, it won't affect the sort order in the view
V. setautoupdate (false );

Document Doc = V. getfirstdocument ();
Document temp = NULL;
// Sets the temp for garbage collection immediately

While (Doc! = NULL)
{
// Do something with the document here...
// Whatever, just don't delete it (yet )!
Temp = V. getnextdocument (DOC); // get the next one
Doc. Recycle (); // recycle the one we're re done
Doc = temp;
}
// End while

} Catch (exception E)
{
E. printstacktrace ();
}
}
}

This article is from the lotus tutorial, Java tutorial, Java virtual machine, JAVA Software Integrated DevelopmentCommunity, Reprinted please indicate the source: http://www.softmobi.cn/index.php/archives/lotusscript-java-recycle.html

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.