Posts

Showing posts from January, 2008

Experience using apache commons EqualsBuilder class

Following are my learnings on how to use the EqualsBuilder class in apache commons lang library.

C:\Work\EqualsProto\src\equalsproto\Main.java

package equalsproto;

//~--- non-JDK imports --------------------------------------------------------

import java.util.Arrays;
import org.apache.commons.lang.builder.EqualsBuilder;


class A
{
private String s = "Watsh";
privateint x = 10;
privatefloat y = 20.2f;

//~--- constructors --------------------------------------------------------

A() {}

A(int i, int i0, String string)
{
this.x = i;
this.y = i0;
this.s = string;
}

//~--- methods -------------------------------------------------------------

/**
* Methoddescription
*
*
* @paramobj

*
* @return
*/
@Override
publicboolean equals(Object obj)
{
if (obj instanceof A == false) {
returnfalse;
}

if (this == obj) {
returntrue;
}

A rhs = (A) obj;

/**Note:

* DonotuseappendSuperwhenthesuperclassisjava.lang.Objectas

* defaultimplementationofequalsinObjectcl…

Detecting memory leaks in Java SE

Today i have learned about a nice approach to detecting memory leaks in the Java SE applications using the jmap and jhat (Java Heap Analysis Tool). The process to be followed is described below:

1. Run your application.

2. Run the command jps to know the process id of the J2SE application you ran.
% jps
1234 MyApp
...

3. Perform those actions in your application which you feel will cause the memory leak. You can observe the real time heap usage plot in jconsole. Launch jconsole and select your application in it to connect to.
% jconsole

4.Then run the command jmap to dump the heap.
% jmap -dump:file=myapp.bin 1234

This will produce a heap dump in myapp.bin file with the heap profile.

4. Run the JHAT (Java Heap Analysis Tool) as follows:
% jhat-J-mx512m heap.bin

The above command starts a small Http server at port 7000 by default.

5. Browse to http://localhost:7000 and you will have you the heap browser - a hyperlinked set of pages from where you can trace every object allocated and who all referenc…