-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: memory usage logger add (#129)
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
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
48 changes: 48 additions & 0 deletions
48
logbat/src/main/java/info/logbat/dev/util/MemoryUsageLogger.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,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); | ||
} | ||
} |