Thursday, 29 March 2012

Developing High Performing XPages Applications

Recently, I come across a strange scenario, where an XPages application was not able to meet the performance testing benchmark of 5sec average load time for more than 300 concurrent users. The application performed like a charm till 100 concurrent users but beyond that the response time was increasing to around 25sec. The expert team performed Code Review to optimize the performance, CPU, IO, Memory Utilization analysis to figure out the issue with Application's response time. I am sharing my experience for the benefit of others as well.. The result that we figured out was related to Memory Leak in "Native API Calls". This indeed is a very important point for almost all the XPages applications.

Following Technotes highlight the importance of using Recycle() method in each Notes Object accessed via Java or SSJS code.

Title:    'LSXBE: Out of backend memory' errors coincide with Notes/Domino performance and crash issues
Doc #:    1438386
URL:    http://www.ibm.com/support/docview.wss?uid=swg21438386

Title:    Why it is important to use the Recycle() method on every Java object
Doc #:    1097861
URL:    http://www.ibm.com/support/docview.wss?uid=swg21097861

Important point to notice is the approach how Java code handles the Notes Objects. Setting a java Object is not sufficient for Domino Java Objects. They must be recycled!

Creating an object in LotusScript:

When you create an object in LotusScript, you create two objects: one in LotusScript and one in the Notes backend (C++ code). The LotusScript object "points" to the C++ object which really implements the class behavior. When the LotusScript agent or event ends, both objects are destroyed automatically. If the objects were not eliminated, they would stay resident in memory, thereby creating a memory leak.
Creating an object in Java:
If you create an object in Java, the same applies. This object is a pointer that points to an object that is created in C++. It is common in pure Java coding to set something to Null in order for it to be flagged for garbage collection (gc) immediately. This is called "aggressive" garbage collection.
However, when using Notes objects for Java, setting something to Null marks the Java object for garbage collection but does not mark the C++ object for garbage collection. Dereferencing the Java object flags it for garbage collection but nothing will be affected on the C++ object.
Why recycle?
Recycle() destroys the C++ object and sets the Java object for garbage collection. If you mark objects Null, you can still experience memory issues. When using objects in an agent, all objects (both Java and C++) are destroyed when the agent ends. When using servlets, .jsp's, or standalone applications, recycle must be used since Domino will never clean up these backend objects.

Following URL contains a nice collection of XPages Performance Tips:

http://xpageswiki.com/web/youatnotes/wiki-xpages.nsf/dx/Memory_Usage_and_Performance

Multi-threaded Programming in XPages

Thanks to the great work done by Niklas Heidloff, Philippe Riand.

We have a great capability to write Multi threaded applications in XPages. A project “Threads and Jobs” has been posted on OpenNTF.org

http://www.openntf.org/internal/home.nsf/project.xsp?action=openDocument&name=Threads%20and%20Jobs

The project is using the The Eclipse jobs framework (http://www.eclipse.org/articles/Article-Concurrency/jobs-api.html) to perform time consuming processes asynchronously while the XPages UI is available for user without freezing.

Here’s a short video posted by Phil: