Automation Blog

from Stefan Schnell


Get Runtime Memory


/**
 * Delivers data about memory
 *
 * @function getRuntimeMemory
 * @returns {void}
 *
 * @author Stefan Schnell <mail@stefan-schnell.de>
 * @license MIT
 * @version 0.1.0
 *
 * Hint: Set the system property
 * com.vmware.scripting.javascript.allow-native-object
 * in the Control Center (Aria Automation 8.*) or
 * System Settings (VCF Automation 9.*) to true.
 */

function getRuntimeMemory() {

  const runtime = java.lang.Runtime.getRuntime();

  // Maximum amount of memory that the JVM machine will attempt to use,
  // limit of memory allocated to the JVM
  const maxMemory = runtime.maxMemory();
  // Total amount of memory in the JVM, memory that the JVM has
  // currently reserved from the OS
  const totalMemory = runtime.totalMemory();
  // Amount of free memory in the JVM
  const freeMemory = runtime.freeMemory();
  // Amount of memory currently actively blocked by the app
  const usedMemory = totalMemory - freeMemory;
  // Amount of memory still available to the app
  const unusedMemory = maxMemory - usedMemory;

  System.log("Maximum memory (-Xmx): " + maxMemory / 1048576 + " MB");
  System.log("Total memory:          " + totalMemory / 1048576 + " MB");
  System.log("Free memory:           " + freeMemory / 1048576 + " MB");
  System.log("Used memory:           " + usedMemory / 1048576 + " MB");
  System.log("Unused memory:         " + unusedMemory / 1048576 + " MB");

}