Class MemoryCheck

java.lang.Object
org.dlese.dpc.util.MemoryCheck

public class MemoryCheck extends Object
This class provides a utility to determine how much memory your application is using at any given time. Useful for determining the memory requirements of a given Object and for debugging possible memory leaks. See JavaWorld article for more info. Includes a sample main method that shows how it might be used.

To get a snapshot of current memory first call runGC() and then call getUsedMemory() .

Example code:

MemoryCheck.runGC();
Long currentMem = MemoryCheck.getUsedMemory();

Version:
$Id: MemoryCheck.java,v 1.6 2009/03/20 23:34:00 jweather Exp $
Author:
John Weatherley
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Return a string for the current time and date, sutiable for display in log files and output to standout:
    static long
    Gets the current amount of memory being used by this app, in bytes.
    static String
    Gets the current amount of memory being used by this app, in megabytes.
    static void
    main(String[] args)
    The main program for the MemoryCheck class
    static void
    Runs object finization and the garbage collector several times.

    Methods inherited from class java.lang.Object

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

    • MemoryCheck

      public MemoryCheck()
  • Method Details

    • main

      public static void main(String[] args) throws Exception
      The main program for the MemoryCheck class
      Parameters:
      args - The command line arguments
      Throws:
      Exception - If error
    • getUsedMemory

      public static long getUsedMemory()
      Gets the current amount of memory being used by this app, in bytes. For best accuracy you should call runGC() prior to calling this method.
      Returns:
      The current amount of memory being used by the JVM.
    • getUsedMemoryInMegs

      public static String getUsedMemoryInMegs()
      Gets the current amount of memory being used by this app, in megabytes. For best accuracy you should call runGC() prior to calling this method.
      Returns:
      The current amount of memory being used by the JVM.
    • runGC

      public static void runGC()
      Runs object finization and the garbage collector several times. This tends to be more effective than juse calling garbage collection once.
    • getDateStamp

      public static String getDateStamp()
      Return a string for the current time and date, sutiable for display in log files and output to standout:
      Returns:
      The dateStamp value