Minimizing memory Leaks in your java j2ee applications | Techartifact

Enough of ADF.Lets talk about memory leak in java today in our application.How we can reduce it.

Java’s memory managmement (i.e Garbage collectionGarbage collection -how it works) prevents lost references and dangling references but it is still possibilities to create memory leaks in other ways. If the application run with memory leaks for long duration ,you will get the error java.lang.OutOfMemoryError.

In java ,typically the memory leak occur when an object of a longer lifecycle has references to the objects of a short life cycle. This prevents the
object with short life cycle being garbage collected. The developer must remember to remove the references to the short lived object from the long-lived objects.Objects with same life cycle do not cause any problem because the garbage collector is smart enough to deal with the circular references.

-> Java collection class like HashTable, ArrayList etc maintain references to other objects.So having a long life cycle ArrayList pointing to many . . short-lifecyle objects can cause memory leaks.

-> Commonly used singleton Design pattern can cause memory leaks.Singleton typically have a long lifecycle. If a singleton has an arrayList or a HashTable then there is potential for memory leaks.

-> Java Programming language includes a finalize method that allows an object to free system resources ,in other words to clean up after itself. However using finalize doesn’t guarantee that a class will clean up resources expediently. A better approach for cleaning up resources involves the finally method and an explicit close statement.so freeing up the valuable resources in the finalize method or try {} block instead of finally() block can cause memory leaks.

Strings and String literals in String Literal Pool

A lot of times I have been asked questions to count the number of string objects created in a statement. Yes, typically by product companies. So it triggered me to actually write an article on it.

Let us discuss on the following topics then

  • String is immutable
  • String pool
  • String literal and garbage collection

String is immutable?

Strings are immutable objects. 

Yes, once created, they cannot be change. Although we can perform operations on it to create new string objects.

 
String str = "a" + "b";

In the statement above, we actually create string object “a”, then string object “b” and then append them to create new string object “c”. We do not alter the original strings “a” and “b”.
Same is the case when we write

 String s1 = "a";
String s2 = s1.concat("b");

String object “a” is created. String object “b” is created and concatenated to “a”. Oh no, String object “a” and “b” are added and new String object “ab” is created. The concat() is hence a misnomer. But one thing worth noting about the concat() according to javadoc is

If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

Fairly simple but worth noting.

 String literal pool

String literal pool is a collection of references to String objects.

But the String objects are themselves created on heap, just like other objects. And the String literal pool references are maintained by the JVM (say in a table).

String literals and String pool

Now since the String objects are immutable, it is safe for multiple references to same literal actually share the same object.

 String s1 = "harsh";
String s2 = "harsh";

The code above seems to create 2 String objects, with literal as “harsh”. But in JVM, both the references s1 and s2 point to the same String object.
How do we test that?

System.out.println("Equals method: " + s1.equals(s2));
System.out.println("==: " + (s1 == s2));

This should explain you what I am trying to say.

What happens behind the scenes is that the string literals are noted down separately by the compiler. When the classloader loads the class, goes through the literal table. When it finds a literal, it searches through the list of String pool references to see if the equivalent String already exists on heap. If one already exists, all the references in class to this String literal are replaced with the reference to the String object on heap (pointed to by the String literal table). If none exists, then a new object is created on heap and then its reference is created on String literal pool table. So any subsequent references to this literal are automatically mapped to the existing String object on heap.

The ‘new’ operator and String literal pool

When it comes to the ‘new’ operator, it forces the JVM to create a new String object on the String literal pool. It has no connection whatsoever with the objects on String literal pool.

A ‘new’ operator creates and points to a new String object on heap.

Hence, do not think ‘String literal pool’ when you encounter ‘new’ operator.

Literal mathematics through constant operations

What about the String created through literal mathematics?
String s1 = "ab" + "c";
String s2 = "a" + "bc";

The above operation also creates a single literal on string pool, and both the references point to it. How? Because compiler can calculate this at compile time, that we are doing string literal constant mathematics.

Literal mathematics through objects

String s1 = "a";
String s2 = s1 + "bc";
String s3 = "a" + "bc";
In statements above, s2 and s3 do not point to the same literal.
i.e. when we perform some mathematics through references, compiler isn’t able to identify the resultant string at compile time, and hence does not make an entry for the literal pool.
String object referenced by s1 and s3 go on the literal pool, but not the one referenced by s2 as it is created at runtime.

String literal garbage collection

An object is eligible for garbage collection when it is no longer referenced.

But our String literals on the literal pool are always referenced by the literal pool. So they are never eligible for garbage collection. They are always accessible through String interns.
But the objects created by ‘new’ operator are eligible if they are no longer referenced – as they are never referred to by the pool.

Navigating from calendar activity by clicking in Oracle ADF – Techartifact

af:calendar component in ADF is widely used.It is same as Google calendar we used. In calendar we are having different activities .We are having different calendar facet inside the calendar component i. e activitydetail, activityDelete, activityHover, activityContextMenu, create , contextMenu.

If you look down the property of calendar component inside property pallet. you will not find action or actionListener property instead of you will get
calendarActivity, calendarListener etc.

Now my requirment is to clicking on the calendar component , it should navigate to diffrent page in same taskflow. How to do that.Little tricky
According to calendar component we can have this navigation easily by the child component or from the calendar facet. We can have a show a popup
in the activityHover facet. In the popup window we can have the button ,inside the button action property we can define the control flow case or some string which we defined in the taskflow diagram for navigation. If you do like this when you hover over the calendar activity a pop up window will come and you will get button with some other detail , when you click the button you will be navigated to new page. look easy…….

But my requirment is navigate after clicking the acitivity . Then i made that button visible property to false .I created a managed bean and inside the activityListener of calendar component i wrote a method and invoke the button action programmatic and queue the action event as i described in my old post

 
        UIComponent component = null;
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (facesContext != null) {
          UIComponent root = facesContext.getViewRoot();
          component = findComponent(root, "buttonId");
        }
        
        
        // UIViewRoot root = facesContext.getViewRoot();
        //cb1 is the fully qualified name of the button
        RichCommandButton button = (RichCommandButton)component;
        ActionEvent actionEvent = new ActionEvent(button);
        actionEvent.queue();