Guidelines for memory management in Contenta Java applications
The following guidelines should be followed when writing or customizing Java code that interacts with the Contenta API
- Contenta API objects created by Java code must be released.
- This includes temporary objects that currently may not be assigned to anything.
- Contenta API objects retrieved/created with
apimgr.get*methods (e.g.,apimgr.getProject) must be released. - Contenta API objects returned from direct API calls, or other pcmweb.jar calls must be released - (for example,
PCMcommand::Select,PCMSync::ListConfigs). - Object must be released during normal code execution and when errors occur. This means that in try/catch blocks, object must be released in the try and the catch blocks. Another approach is to use the finally clause and do the release once there since the finally clause is always executed.
- CWSession Notes (This is specific to CW customizations/tools)
- The CWSession and CWServletListener objects are responsible for releasing any Contenta objects that are stored on the session. Each Contenta Web web application creates a listener (CWServletListener) which implements the HttpSessionListener, HttpSessionAttributeListener, and ServletContextListener interfaces. The listener object for each web application is created via the deployment descriptor for the web application (web.xml). This class is used to facilitate the releasing of Contenta API objects when they are removed from the session. Below is the text from web.xml for the CW application.
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>cw</display-name> <description>Contenta Web Application</description> <!-- Listeners --> <listener> <listener-class>com.xyenterprise.PCMWeb.CWServletListener</listener-class> </listener> <!-- Session timeout (uncomment the lines below to configure the session timeout for this web application)--> <!-- <session-config> <session-timeout>30</session-timeout> </session-config> --> </web-app> - All the CWSession methods (e.g.,
connectGetCommand,getConnection,getCmd...) that return Contenta API objects store the API objects on the session, and the CWSession and CWServletListener objects take care of releasing them. - If a Contenta API object is stored on the session and never removed – the CWSession and CWServletListener objects will release it during disconnect/cleanup/timeout.
- If code manually removes a Contenta API object from a session via the
CWSession::removeAttributemethod – the session object will automatically release the Contenta API object. - If code replaces a Contenta API object on the session by using the
CWSession::setAttributemethod – the object being replaced will automatically be released by the CWSession and CWServletListener objects. However, if theCWSession::setAttributemethod is used to replace a Contenta API object on the session with the same Contenta API object, the existing Contenta API object will NOT be released.
- The CWSession and CWServletListener objects are responsible for releasing any Contenta objects that are stored on the session. Each Contenta Web web application creates a listener (CWServletListener) which implements the HttpSessionListener, HttpSessionAttributeListener, and ServletContextListener interfaces. The listener object for each web application is created via the deployment descriptor for the web application (web.xml). This class is used to facilitate the releasing of Contenta API objects when they are removed from the session. Below is the text from web.xml for the CW application.