2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.common
;
11 import java
.lang
.management
.GarbageCollectorMXBean
;
12 import java
.lang
.management
.ManagementFactory
;
13 import java
.lang
.management
.MemoryMXBean
;
14 import java
.lang
.management
.MemoryUsage
;
15 import java
.util
.List
;
17 import org
.apache
.logging
.log4j
.LogManager
;
18 import org
.apache
.logging
.log4j
.Logger
;
21 * @author a.kohlbecker
24 public class JvmMonitor
{
26 public static final Logger logger
= LogManager
.getLogger();
28 private long gcTimeLast
= 0;
30 private long lastCheckTime
= 0;
33 * Returns the sum of approximate accumulated collection elapsed time in milliseconds
34 * as reported by all garbage collectors.
36 * This method returns -1 if the collection elapsed time is undefined.
40 public long gcTime() {
41 List
<GarbageCollectorMXBean
> gcMXBeans
= ManagementFactory
.getGarbageCollectorMXBeans();
43 //logger.setLevel(Level.DEBUG);
47 for(GarbageCollectorMXBean gcMXBean
: gcMXBeans
){
51 collectorGcTime
= gcMXBean
.getCollectionTime();
52 logger
.debug("cgMxBean: " + gcMXBean
.getName()
53 + " gcTime = " + collectorGcTime
54 + " gcCount = " + gcMXBean
.getCollectionCount());
55 if(collectorGcTime
!= -1) {
56 // only sum up if the time is defined
57 gcTimeSum
+= gcMXBean
.getCollectionTime();
60 logger
.debug("gcTimeSum = " + gcTimeSum
);
65 public MemoryUsage
getHeapMemoryUsage(){
67 MemoryMXBean memoryMXBean
= ManagementFactory
.getMemoryMXBean();
68 if(memoryMXBean
!= null){
69 logger
.debug("HeapMemoryUsage: " + memoryMXBean
.getHeapMemoryUsage());
70 return memoryMXBean
.getHeapMemoryUsage();
76 public boolean hasFreeHeap(long freeHeapLimit
) {
78 if(!_hasFreeHeap(freeHeapLimit
)) {
79 Runtime
.getRuntime().gc();
80 return _hasFreeHeap(freeHeapLimit
);
85 private boolean _hasFreeHeap(long freeHeapLimit
) {
86 long freeHeap
= getFreeHeap(false);
87 return freeHeap
> freeHeapLimit
;
90 public long getFreeHeap(boolean gcBeforeMeasure
) {
92 Runtime
.getRuntime().gc();
94 MemoryUsage heapUsage
= getHeapMemoryUsage();
95 long freeHeap
= heapUsage
.getMax() - heapUsage
.getUsed();
100 * Returns the gcTime in milliseconds as obtained through {@link #gctime()} of the
101 * time interval since this method has been called the last time and now.
105 public long getGCtimeSiceLastCheck() {
106 long gcTimeNow
= gcTime();
107 long gcTimeSince
= gcTimeNow
- gcTimeLast
;
108 gcTimeLast
= gcTimeNow
;
109 lastCheckTime
= System
.currentTimeMillis();
114 * Returns the time spend in gc as proportion (0.0 to 1.0) of the
115 * time interval since this method has been called the last time and now.
119 public double getGCRateSiceLastCheck() {
121 long gcTimeSince
= getGCtimeSiceLastCheck();
122 long timeDiff
= System
.currentTimeMillis() - lastCheckTime
;
123 double gcRate
= gcTimeSince
/ (double) timeDiff
;