diff --git a/base/src/main/java/de/staropensource/sosengine/base/data/info/JvmInformation.java b/base/src/main/java/de/staropensource/sosengine/base/data/info/JvmInformation.java index 5ef36de..d5be799 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/data/info/JvmInformation.java +++ b/base/src/main/java/de/staropensource/sosengine/base/data/info/JvmInformation.java @@ -25,6 +25,7 @@ import de.staropensource.sosengine.base.types.CodePart; import org.jetbrains.annotations.NotNull; import java.lang.management.ManagementFactory; +import java.lang.management.MemoryUsage; import java.util.List; /** @@ -117,4 +118,71 @@ public final class JvmInformation { public static List<@NotNull String> getArguments() { return ManagementFactory.getRuntimeMXBean().getInputArguments(); } + + /** + * Returns the total amount of memory used by the JVM and the running application. + *

+ * Note: This is an estimate. + * + * @return estimated total amount of used memory + * @since 1-alpha1 + */ + public static long getMemoryTotal() { + return Runtime.getRuntime().totalMemory(); + } + + /** + * Returns the maximum amount of memory usable by the running application. + * + * @return maximum amount of usable memory + * @since 1-alpha1 + */ + public static long getMemoryMax() { + return Runtime.getRuntime().maxMemory(); + } + + /** + * Returns the amount of free memory available to the running application. + * + * @return amount of free memory + * @since 1-alpha1 + */ + public static long getMemoryFree() { + return Runtime.getRuntime().freeMemory(); + } + + /** + * Returns the memory usage of the heap. + * + * @return heap usage + * @since 1-alpha1 + */ + public static MemoryUsage getMemoryHeap() { + return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); + } + + /** + * Returns the memory usage of the stack. + * + * @return stack usage + * @since 1-alpha1 + */ + public static MemoryUsage getMemoryStack() { + return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage(); + } + + /** + * Returns the amount of processors used by the JVM. + *

+ * Note: The amount of processors used may change rapidly. + * If your application scales resources based on the output of + * this method, consider checking it's output often and scaling + * resources accordingly. + * + * @return amount of available processors + * @since 1-alpha1 + */ + public static int getAvailableProcessors() { + return Runtime.getRuntime().availableProcessors(); + } }