Class InstanceCounter


  • public class InstanceCounter
    extends Object
    Counts instances of a particular class, helpful in leak detection

    Example: Put the following line into the class you want counted:

    private final InstanceCounter instanceCounter = new InstanceCounter<>(this);

    Author:
    Cuba Stanley
    • Constructor Detail

      • InstanceCounter

        public InstanceCounter​(Object counted)
        triggers instance counting of the specified
        Parameters:
        counted - object to be counted
    • Method Detail

      • getInstanceCount

        public static int getInstanceCount​(Class<?> countedType,
                                           long timeout)
        Returns instance count of a particular class.
        Parameters:
        countedType -
        timeout - in milliseconds to wait to collect removed instances
        Returns:
        current instance count
      • getInstances

        public static Set<WeakReference<?>> getInstances​(Class<?> countedType,
                                                         long timeout)
        gets actual references to counted objects
        Parameters:
        countedType -
        timeout - in milliseconds
        Returns:
        list of counted objects