Saturday, 1 June 2019

How to count number of instances of a class?


You can count number of instances of a class by creating a static file ‘counter’ and increment it whenever an object is instantiated.

Employee.java
package com.sample.app;

public class Employee {

 private static int noOfEmployees = 0;

 public Employee() {
  noOfEmployees++;
 }

 public static int getNoOfEmployees() {
  return noOfEmployees;
 }

}


App.java
package com.sample.app;

public class App {

 public static void main(String args[]) {
  Employee emp1 = new Employee();
  Employee emp2 = new Employee();

  System.out.println("Total Employee objects : " + Employee.getNoOfEmployees());
 }
}

Output
Total Employee objects : 2

What if the unreachable objects are garbage collected? How can you track the number of non-garbage collected objects?
It is simple, Override finalize() method and decrement the counter.

@Override
protected void finalize() throws Throwable {
         super.finalize();
         noOfEmployees--;
}


Employee.java
package com.sample.app;

public class Employee {

 private static int noOfEmployees = 0;

 public Employee() {
  noOfEmployees++;
 }

 public static int getNoOfEmployees() {
  return noOfEmployees;
 }

 @Override
 protected void finalize() throws Throwable {
  super.finalize();
  noOfEmployees--;
 }
}


If you want to track number of objects from outside of the application, you can use ‘jmap -histo <PDID>’ command.

$jmap
Usage:
    jmap [option] <pid>
        (to connect to running process)
    jmap [option] <executable <core>
        (to connect to a core file)
    jmap [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    <none>               to print same info as Solaris pmap
    -heap                to print java heap summary
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects
    -clstats             to print class loader statistics
    -finalizerinfo       to print information on objects awaiting finalization
    -dump:<dump-options> to dump java heap in hprof binary format
                         dump-options:
                           live         dump only live objects; if not specified,
                                        all objects in the heap are dumped.
                           format=b     binary format
                           file=<file>  dump heap to <file>
                         Example: jmap -dump:live,format=b,file=heap.bin <pid>
    -F                   force. Use with -dump:<dump-options> <pid> or -histo
                         to force a heap dump or histogram when <pid> does not
                         respond. The "live" suboption is not supported
                         in this mode.
    -h | -help           to print this help message
    -J<flag>             to pass <flag> directly to the runtime system


You may like


No comments:

Post a Comment