Class MemoryUtil


  • public final class MemoryUtil
    extends java.lang.Object
    Memory related utilities.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int SAMPLING_SIZE  
      private static int SLEEP_TIME  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private MemoryUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void gc()  
      static long getUsedMemory()
      Returns the currently used memory, after calling garbage collector and waiting enough to get maximal chance it is actually called.
      static void main​(java.lang.String[] args)  
      static long sizeOf​(java.lang.Class<?> clazz)
      Returns the approximate size of a default instance of the given class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MemoryUtil

        private MemoryUtil()
    • Method Detail

      • sizeOf

        public static long sizeOf​(java.lang.Class<?> clazz)
        Returns the approximate size of a default instance of the given class.
        Parameters:
        clazz - the class to evaluate.
        Returns:
        the estimated size of instance, in bytes.
      • getUsedMemory

        public static long getUsedMemory()
        Returns the currently used memory, after calling garbage collector and waiting enough to get maximal chance it is actually called. But since Runtime.gc() is only advisory, results returned by this method should be treated as rough approximation only.
        Returns:
        the currently used memory, in bytes.
      • gc

        private static void gc()
      • main

        public static void main​(java.lang.String[] args)
                         throws java.lang.ClassNotFoundException
        Throws:
        java.lang.ClassNotFoundException