Skip to content

Commit

Permalink
add memory logs
Browse files Browse the repository at this point in the history
  • Loading branch information
notshivansh committed Jan 20, 2025
1 parent c798760 commit 024344d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.akto.log.LoggerMaker;
import com.akto.log.LoggerMaker.LogDb;
import com.akto.metrics.AllMetrics;
import com.akto.monitor.MemoryMonitor;
import com.akto.sql.SampleDataAltDb;
import com.akto.hybrid_parsers.HttpCallParser;
import com.akto.data_actor.DataActor;
Expand Down Expand Up @@ -150,6 +151,9 @@ public static String getTopicName(){
return topicName;
}

private static final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();


// REFERENCE: https://www.oreilly.com/library/view/kafka-the-definitive/9781491936153/ch04.html (But how do we Exit?)
public static void main(String[] args) {
//String mongoURI = System.getenv("AKTO_MONGO_CONN");;
Expand Down Expand Up @@ -303,7 +307,7 @@ public void run() {

long lastSyncOffset = 0;

Map<Integer, Integer> logSentMap = new HashMap<>();
executorService.scheduleAtFixedRate(new MemoryMonitor.MemoryMonitorTask(), 0, 10, TimeUnit.SECONDS);

try {
main.consumer.subscribe(Arrays.asList(topicName, "har_"+topicName));
Expand Down
36 changes: 36 additions & 0 deletions libs/utils/src/main/java/com/akto/monitor/MemoryMonitor.java
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
}
}
}
}

}

0 comments on commit 024344d

Please sign in to comment.