Using the tool jconsole in JDK1.5

The JMX compliant tool jconsole comes with JDK1.5. It allows to manage the VM, check the memory usage of the different memory managers, monitor the thread usage, see how many classes are loaded, etc.

The simplest way to use jconsole is to start up the Java application a JMX agent:

   java -Dcom.sun.management.jmxremote Main

Then, specify the process ID of the Java application when starting up jconsole:

   c:j2sdk1.5.0binjconsole <pid>

To find out the process ID, you can use the ps command on UNIX and the Task Manager on Windows.

There is a workaround to quickly find out the process ID. This following Java application prints out its own process ID and then infinitely starts creating threads that on their turn fill up the memory by creating a number of arrays.

Main.java:

import java.util.*;
import java.lang.reflect.*;
import java.lang.management.*;
 
public class Main
{
   public static void main(String []args) {
      try {
         System.out.println("Process-id: " + getProcessId());
      }
      catch(Exception e) { }
    
      while (true) {
         new Thread(new Task()).start();
          
         try { Thread.sleep(500); } catch(InterruptedException e) { }
      }
   }
    
   public static int getProcessId() throws Exception {
      // next line to trigger loading of library
      ManagementFactory.getThreadMXBean();
       
      Class c = Class.forName("sun.management.VMManagementImpl");
         
      Constructor constructor = c.getDeclaredConstructor(new Class[] { });
      constructor.setAccessible(true);
      Object impl = constructor.newInstance(new Object[] { });
       
      Method m = c.getDeclaredMethod("getProcessId", new Class[] { });
      m.setAccessible(true);
      Object result = m.invoke(impl, new Object[] { });
      
      return (Integer) result;
   }   
}
 
class Task implements Runnable
{
   public void run() {
      Vector<byte[]> v = new Vector<byte[]>();
      while (true) {
         v.add(new byte[10000]);
          
         try { Thread.sleep(100); } catch(InterruptedException e) { }
      }
   }
}

outputs:

Process-id: 2492
... (fills up heap space and eventually throws an OutOfMemoryException)

Then start up the jconsole from the command-line as follows:

   c:j2sdk1.5.0binjconsole 2492

Remote Management

You can also remotely connect. Passwords can be specified and SSL can be enabled (both are default). To start up the Java application without passwords and with SSL disabled, execute this:

java -Dcom.sun.management.jmxremote.port=1097 -Dcom.sun.management.jmxremote.ssl=false 
                                              -Dcom.sun.management.jmxremote.authenticate=false Main

Then, when jconsole starts up specify the host name and port to connect.

You can also use SSL and passwords and roles (like readonly, readwrite). Check the jconsole documentation to do so.