Memory utilization and timers

Two of the most basic profiling tactics are timers and memory analyzers. I have created a cheesy little set of classes that have come in handy more than a few times in these areas. The timer provides basics such as starting, stopping, and splitting time in milliseconds. It also allows for variable length precision, unit scaling (milliseconds, seconds, minutes, hours, and days), output as double or String, and timestamp display. The memory module contains methods to determine Java heap max, total, free, and used values as double or String. As with the timer, the precision and units (bytes, kilobytes, megabytes, and gigabytes) are alterable.

Download: SystemUtils.zip – includes the jar and Javadoc (click download in the upper right-hand corner of the Google Drive window).

Example usage:

This class highlights some of the basics for the timer and memory classes. Timer examples include starting, stopping, and outputting in String and double in milliseconds. Memory instances cover max (String and double), total, free, and used values in megabytes.

import edu.uiowa.icts.ray.systemutils.Memory;
import edu.uiowa.icts.ray.systemutils.SystemUtils;
import edu.uiowa.icts.ray.systemutils.Timer;
import static java.lang.System.out;
 
public class SystemUtilsExample{
    public static void main(String[] args) {
        SystemUtils sys = new SystemUtils();    // instantiate SystemUtil
        Memory mem = sys.newMemory();           // get memory instance
        Timer timer = sys.newTimer();           // get timer instance
        timer.start();                          // start the timer
         
        // print current timestamp
        out.println("Begin process - " + timer.getTimestamp());
         
        /* PROCESS DATA HERE */
         
        // stop the timer and print elapsed time as a string and double in MS
        timer.stop();
        Timer.UNITS tUnits = Timer.UNITS.MS;
        out.println("execution time: " + timer.getDifferenceAsString(tUnits));
        out.println("execution time: " + timer.getDifference(tUnits));
         
        // print various memory values - examples convert to MB
        Memory.UNITS mUnits = Memory.UNITS.MB;
        out.println("max:  " + mem.getMaximumAsString(mUnits));
        out.println("max:  " + mem.getMaximum(mUnits));
        out.println("tot:  " + mem.getTotalAllocatedAsString(mUnits));
        out.println("free: " + mem.getFreeAsString(mUnits));
        out.println("used: " + mem.getUsedAsString(mUnits));
    }
}

Output from the example class:

Begin process - 2015-11-30 16:37:44:522
execution time: 22 MS
execution time: 22.0
max:  3,572 MB
max:  3572.0
tot:  241.5 MB
free: 237.72 MB
used: 3.78 MB