-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c798760
commit 024344d
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
libs/utils/src/main/java/com/akto/monitor/MemoryMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.akto.monitor; | ||
|
||
public class MemoryMonitor { | ||
|
||
// Runnable task for monitoring memory | ||
public static class MemoryMonitorTask implements Runnable { | ||
@Override | ||
public void run() { | ||
Runtime runtime = Runtime.getRuntime(); | ||
|
||
// Loop to print memory usage every 1 second | ||
while (true) { | ||
// Calculate memory statistics | ||
long totalMemory = runtime.totalMemory(); | ||
long freeMemory = runtime.freeMemory(); | ||
long usedMemory = totalMemory - freeMemory; | ||
|
||
// Print memory statistics | ||
System.out.print("Used Memory: " + (usedMemory / 1024 / 1024) + " MB "); | ||
System.out.print("Free Memory: " + (freeMemory / 1024 / 1024) + " MB "); | ||
System.out.print("Total Memory: " + (totalMemory / 1024 / 1024) + " MB "); | ||
System.out.print("Available Memory: " + ((runtime.maxMemory() - usedMemory) / 1024 / 1024) + " MB "); | ||
System.out.println("-------------------------"); | ||
|
||
// Pause for 1 second | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
System.err.println("Memory monitor thread interrupted: " + e.getMessage()); | ||
break; // Exit the loop if thread is interrupted | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |