Sunday Dec 02, 2012

Identify memory leak in Java app

One important advantage of java is programer don't care memory management and GC handle it well. Maybe this is one reason why java is more popular.

As Java programer you real dont care it? After you meet Out of memory you will realize it it’s not true.

Java GC and memory is big topic you can get some information in here

Today just let me show how to identify memory leak quickly.

Let quickly review demo java code, it’s one kind of memory leak in our code, using static collection and always add some object.

import java.util.ArrayList;
import java.util.List;

public class MemoryTest {
public static void main(String[] args) {
new Thread(new MemoryLeak(), "MemoryLeak").start();
}
}

class MemoryLeak implements Runnable {
public static List<Integer> leakList = new ArrayList<Integer>();
public void run() {
int num =0;
while(true) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
num++;
Integer i = new Integer(num);
leakList.add(i);

}
}
}

run it with java -verbose:gc -XX:+PrintGCDetails -Xmx60m -XX:MaxPermSize=160m MemoryTest

after about some minuts you will get

Exception in thread "MemoryLeak" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at MemoryLeak.run(MemoryTest.java:25)
at java.lang.Thread.run(Thread.java:619)
Heap
def new generation total 18432K, used 3703K [0x045e0000, 0x059e0000, 0x059e0000)
eden space 16384K, 22% used [0x045e0000, 0x0497dde0, 0x055e0000)
from space 2048K, 0% used [0x055e0000, 0x055e0000, 0x057e0000)
to space 2048K, 0% used [0x057e0000, 0x057e0000, 0x059e0000)
tenured generation total 40960K, used 40959K [0x059e0000, 0x081e0000, 0x081e0000)
the space 40960K, 99% used [0x059e0000, 0x081dfff8, 0x081e0000, 0x081e0000)
compacting perm gen total 12288K, used 2083K [0x081e0000, 0x08de0000, 0x10de0000)
the space 12288K, 16% used [0x081e0000, 0x083e8c50, 0x083e8e00, 0x08de0000)
No shared spaces configured.

OK let us quickly identify it using JProfile

Download JProfile in here 

Run JProfile and attach MemoryTest get largest size of  Objects in Memory View in here is Integer


then select Integer and go to Heap Walker.

get GC Graph for this object


 Then you get detail code raise this issue quickly now.

 That is enjoy it.

Friday Nov 30, 2012

Identify high CPU consumed thread for Java app

Following java code to emulate busy and Idle thread and start it.

import java.util.concurrent.*;
import java.lang.*;

public class ThreadTest {
    public static void main(String[] args) {
        new Thread(new Idle(), "Idle").start();
        new Thread(new Busy(), "Busy").start();
    }
}

class Idle implements Runnable {

    @Override
    public void run() {
        try {
            TimeUnit.HOURS.sleep(1);
        } catch (InterruptedException e) {
        }
    }
}

class Busy implements Runnable {
    @Override
    public void run() {
        while(true) {
            "Test".matches("T.*");
        }
    }
}

Using Processor Explorer to get this busy java processor and get Thread id it cost lots of CPU

see the following screenshot:

Cover to 4044 to Hexadecimal is oxfcc.

Using VistulVM to dump thread and get that thread.

see the following screenshot

In Linux you can use  top -H to get Thread information.

That it!

Any question let me know. Thanks




About

here is technology share in working and investigation

Search

Top Tags
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today