Skip to content

Commit

Permalink
feat: memory usage logger add (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizyHub authored Aug 27, 2024
1 parent d12b400 commit 4da662d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions logbat/src/main/java/info/logbat/LogbatApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class LogbatApplication {

public static void main(String[] args) {
Expand Down
48 changes: 48 additions & 0 deletions logbat/src/main/java/info/logbat/dev/util/MemoryUsageLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package info.logbat.dev.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;

@Component
public class MemoryUsageLogger {

private static final Logger logger = LoggerFactory.getLogger(MemoryUsageLogger.class);
private final MemoryMXBean memoryMXBean;

public MemoryUsageLogger() {
this.memoryMXBean = ManagementFactory.getMemoryMXBean();
}

@Scheduled(fixedRate = 1000)
public void logMemoryUsage() {
// Heap 메모리 사용량
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
long usedHeapMemory = heapMemoryUsage.getUsed();
long maxHeapMemory = heapMemoryUsage.getMax();
long committedHeapMemory = heapMemoryUsage.getCommitted();

// Non-Heap 메모리 사용량
MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
long usedNonHeapMemory = nonHeapMemoryUsage.getUsed();
long maxNonHeapMemory = nonHeapMemoryUsage.getMax();
long committedNonHeapMemory = nonHeapMemoryUsage.getCommitted();

// Finalization 대기 객체 수
int pendingFinalizationCount = memoryMXBean.getObjectPendingFinalizationCount();

logger.info("Memory Usage [Heap: Used={} MB, Max={} MB, Committed={} MB | Non-Heap: Used={} MB, Max={} MB, Committed={} MB | Pending Finalizations: {}]",
usedHeapMemory / 1024 / 1024,
maxHeapMemory / 1024 / 1024,
committedHeapMemory / 1024 / 1024,
usedNonHeapMemory / 1024 / 1024,
maxNonHeapMemory / 1024 / 1024,
committedNonHeapMemory / 1024 / 1024,
pendingFinalizationCount);
}
}

0 comments on commit 4da662d

Please sign in to comment.