Tuesday Jan 06, 2009

Managing Java Process from Java Code

Back to Java :). Here is a small blog on how to manage Java Process from Java itself.

Long back, I had written one blog on how to list Java Process running on System by Java Code. But with the new features of JDK6, you can not only see the list but can manage the other running Java Process. This is possible using class LocalVirtualMachine. This class has a list of methods :

connectorAddress,
displayName,
getAllVirtualMachines,
getLocalVirtualMachine,
isAttachable,
isManageable,
startManagementAgent,
toString,
vmid

Here I am just showing a simple code, which will again tell you all the running Java Process.

import sun.tools.jconsole.\*;
import java.util.\*;
public class Main {

public static void main(String[] args) {
Map map = LocalVirtualMachine.getAllVirtualMachines();
Iterator iter = map.values().iterator();
LocalVirtualMachine vm = null;
while (iter.hasNext()) {
vm = (LocalVirtualMachine)iter.next();
System.out.println(vm.displayName());
}
}
}

A very very small code :). Note that this class is not in rt.jar so we need to add jconsole.jar and tools.jar in the classpath section.
So, for running we need to use :

D:/Program Files/Java/jdk1.6.0_11/bin/ControlJavaApp>../javac -cp "D:/Program Fi
les/Java/jdk1.6.0_11/lib/jconsole.jar" Main.java

D:/Program Files/Java/jdk1.6.0_11/bin/ControlJavaApp>../java -cp .;"D:/Program F
iles/Java/jdk1.6.0_11/libjconsole.jar";"D:Program Files/Java/jdk1.6.0_11/lib/t
ools.jar" Main

Or we can modify classpath in Env. Variables.

Right now, in my system it is displaying:

Main
org/netbeans/modules/javafx/preview/Main 1
That means, this code itself and Netbeans is running as a java process.

In next blog, we will try to see how to manage(not listing) other running java code from a java code. I am not able to find too much of doc from net, so things are slow :).

Saturday Mar 08, 2008

Profiling with Netbeans 6

Working on industrial application,profiling project is one of the major
challenge. There are some of the profiling tools shipped with JDK like
jhat, jmap, jconsole. But week back, I start looking the profiling
power of Netbeans which is quite better than JDK tools. It provides
much better and relevant tools for profiling. Don't mind if it will not
able to connect VM sometime :). Here I have written one small code for
deadlock:



class DeadLockTest {
public static void main(String[] args) {
    Object lock1 = new String("Lock 1");
    Object lock2 = new String("Lock 2");
    new DeadLockCondition("t1", lock1, lock2);

    new DeadLockCondition("t2", lock2, lock1);
    }

}
class DeadLockCondition extends Thread {
Object lock1;

Object lock2;
DeadLockCondition(String name, Object lock1, Object lock2)

{

    super(name);

    this.lock1 = lock1;

    this.lock2 = lock2;

    start();

}
public void run() {

    while(true) {

        method();

    }

}

public void method() {

    synchronized(lock1) {

        method2();

    }

}
public void method2() {

    int count = 0;

    synchronized(lock2) {

        while(count<100000){

        count++;

        System.out.println(count);

        }

    }
}
}


Now I have started Netbeans Profiling. The best part is CPU profiling
which allow you to profile parts of project, no need to profile whole
project. It will show you heap memory, GC memory, classes loaded like:

 

 

Here you can definitely see how threads are waiting for each other in Thread view.

 


Now take the heap dump from Profiling option. It will by default open your heap dump in heap walker. Now in heap walker you can see classes, instances, size and lot many things. From classes we can directly go to the source code. See this:

 

 



And here I go in the source :)

 

 



There is lot to do here. But I am sick of pasting this image. Go ahead, and explore it :), it's worthwhile to spend some time. Right now, I am working on a printer memory leak problem. Will write more if able to solve with Profiling.


About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

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