org.whitesource.utils.MemoryUsageHelper Maven / Gradle / Ivy
/**
* The project is licensed under the WhiteSource Software End User License Agreement .
* Copyright (C) 2015 WhiteSource Ltd.
* Licensed under the WhiteSource Software End User License Agreement;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://s3.amazonaws.com/unified-agent/LICENSE.txt
*/
package org.whitesource.utils;
/**
* @author eugen.horovitz
* Class helper for tracking memory usage
*
*/
public class MemoryUsageHelper {
/**
*
* @return Gets memory usage
*/
public static SystemStats getMemoryUsage() {
long mbRatio = 1024*1024;
Runtime runTime = Runtime.getRuntime();
return new SystemStats(runTime.availableProcessors(),runTime.freeMemory()/mbRatio,
runTime.maxMemory()/mbRatio,runTime.totalMemory()/mbRatio,
(runTime.totalMemory()-runTime.freeMemory())/mbRatio);
}
public static class SystemStats {
int availableProcessors;
long freeMemory;
long maxMemory;
long totalMemory;
long usedMemory;
private SystemStats(int availableProcessors, long freeMemory, long maxMemory, long totalMemory, long usedMemory) {
this.availableProcessors = availableProcessors;
this.freeMemory = freeMemory;
this.maxMemory = maxMemory;
this.totalMemory = totalMemory;
this.usedMemory = usedMemory;
}
public int getAvailableProcessors() {
return availableProcessors;
}
public long getFreeMemory() {
return freeMemory;
}
public long getMaxMemory() {
return maxMemory;
}
public long getTotalMemory() {
return totalMemory;
}
public long getUsedMemory() {
return usedMemory;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
/* Total number of processors or cores available to the JVM */
sb.append("Available processors (cores): \t" + getAvailableProcessors());
sb.append(System.lineSeparator());
/* Total amount of free memory available to the JVM */
sb.append("Free memory (Mb): \t" +
getFreeMemory());
sb.append(System.lineSeparator());
/* This will return Long.MAX_VALUE if there is no preset limit */
/* Maximum amount of memory the JVM will attempt to use */
sb.append("Max memory (Mb): \t" + (getMaxMemory() == Long.MAX_VALUE / 1024 / 1024 ? "no limit" : getMaxMemory()));
sb.append(System.lineSeparator());
/* Total memory currently in use by the JVM */
sb.append("Total memory (Mb): \t" + getTotalMemory());
sb.append(System.lineSeparator());
/* Used memory currently in use by the JVM */
sb.append("Used memory (Mb): \t" + getUsedMemory());
sb.append(System.lineSeparator());
return sb.toString();
}
}
}