Friday 10 August 2018

Call 'java.lang.System.runFinalization()' using jcmd command

HelloWorld.java
public class HelloWorld {
 public static void main(String args[]) throws Exception {
  Thread t1 = new Thread() {
   public void run() {
    int count = 0;
    while (true) {
     count++;

     Employee emp = new Employee();
     emp.id = count;
     emp.name = "name " + count;

     try {
      Thread.sleep(1000);
     } catch (Exception e) {

     }
    }
   }
  };

  t1.start();
 }

 public static class Employee {
  private int id;
  private String name;

  @Override
  protected void finalize() throws Throwable {
   System.out.println("Calling finalize method for id : " + id + ", name : " + name);
  }
 }
}


Compile and run HelloWorld application.



Open command prompt and execute ‘jcmd’ command.
C:\>jcmd
13220
772 sun.tools.jconsole.JConsole
19880 HelloWorld
6824 sun.tools.jcmd.JCmd


Execute the command 'jcmd 19880 help' to see the available commands for the process id 19880.


As you see above image, 'GC.run_finalization' command is available for the process id 19880.

Call the command 'GC.run_finalization'.
C:\>jcmd 19880 GC.run_finalization
19880:
Command executed successfully



Previous                                                 Next                                                 Home

No comments:

Post a Comment