-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathHeapStatus.java
37 lines (31 loc) · 1.33 KB
/
HeapStatus.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
/**
* This program returns the following three memory values
* <p>
* Returns the amount of used memory in bytes.
* <p>
* Returns the amount of memory in bytes that is committed for the Java virtual machine to use.
* This amount of memory is guaranteed for the Java virtual machine to use
* <p>
* Returns the maximum amount of memory in bytes that can be used for memory management.
* This method returns -1 if the maximum memory size is undefined.
*
* @author rajeshp
* @Date 10/17/22
*/
public class HeapStatus {
// 1 Megabyte is equal to 1048576 bytes (binary)
public static final long MEGABYTE = 1048576;
public static void main(String[] args) {
//Returns the managed bean for the memory system of the Java virtual machine.
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
System.out.println(String
.format("Heap: %sM used, %sM commited, %sM max",
memoryMXBean.getHeapMemoryUsage().getUsed()
/ MEGABYTE, memoryMXBean
.getHeapMemoryUsage().getCommitted()
/ MEGABYTE, memoryMXBean
.getHeapMemoryUsage().getMax() / MEGABYTE));
}
}