1
|
/**
|
2
|
* Copyright (C) 2016 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.common;
|
10
|
|
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;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
|
19
|
/**
|
20
|
* @author a.kohlbecker
|
21
|
* @since Jul 1, 2016
|
22
|
*/
|
23
|
public class JvmMonitor {
|
24
|
|
25
|
public static final Logger logger = Logger.getLogger(JvmMonitor.class);
|
26
|
|
27
|
private long gcTimeLast = 0;
|
28
|
|
29
|
private long lastCheckTime = 0;
|
30
|
|
31
|
/**
|
32
|
* Returns the sum of approximate accumulated collection elapsed time in milliseconds
|
33
|
* as reported by all garbage collectors.
|
34
|
*
|
35
|
* This method returns -1 if the collection elapsed time is undefined.
|
36
|
*
|
37
|
* @return
|
38
|
*/
|
39
|
public long gcTime() {
|
40
|
List<GarbageCollectorMXBean> gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
|
41
|
|
42
|
//logger.setLevel(Level.DEBUG);
|
43
|
|
44
|
long gcTimeSum = -1;
|
45
|
long collectorGcTime;
|
46
|
for(GarbageCollectorMXBean gcMXBean : gcMXBeans){
|
47
|
if(gcTimeSum == -1) {
|
48
|
gcTimeSum = 0;
|
49
|
}
|
50
|
collectorGcTime = gcMXBean.getCollectionTime();
|
51
|
logger.debug("cgMxBean: " + gcMXBean.getName()
|
52
|
+ " gcTime = " + collectorGcTime
|
53
|
+ " gcCount = " + gcMXBean.getCollectionCount());
|
54
|
if(collectorGcTime != -1) {
|
55
|
// only sum up if the time is defined
|
56
|
gcTimeSum += gcMXBean.getCollectionTime();
|
57
|
}
|
58
|
}
|
59
|
logger.debug("gcTimeSum = " + gcTimeSum);
|
60
|
return gcTimeSum;
|
61
|
|
62
|
}
|
63
|
|
64
|
public MemoryUsage getHeapMemoryUsage(){
|
65
|
|
66
|
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
|
67
|
if(memoryMXBean != null){
|
68
|
logger.debug("HeapMemoryUsage: " + memoryMXBean.getHeapMemoryUsage());
|
69
|
return memoryMXBean.getHeapMemoryUsage();
|
70
|
}
|
71
|
return null;
|
72
|
}
|
73
|
|
74
|
|
75
|
public boolean hasFreeHeap(long freeHeapLimit) {
|
76
|
|
77
|
if(!_hasFreeHeap(freeHeapLimit)) {
|
78
|
Runtime.getRuntime().gc();
|
79
|
return _hasFreeHeap(freeHeapLimit);
|
80
|
}
|
81
|
return true;
|
82
|
}
|
83
|
|
84
|
private boolean _hasFreeHeap(long freeHeapLimit) {
|
85
|
long freeHeap = getFreeHeap(false);
|
86
|
return freeHeap > freeHeapLimit;
|
87
|
}
|
88
|
|
89
|
public long getFreeHeap(boolean gcBeforeMeasure) {
|
90
|
if(gcBeforeMeasure) {
|
91
|
Runtime.getRuntime().gc();
|
92
|
}
|
93
|
MemoryUsage heapUsage = getHeapMemoryUsage();
|
94
|
long freeHeap = heapUsage.getMax() - heapUsage.getUsed();
|
95
|
return freeHeap;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Returns the gcTime in milliseconds as obtained through {@link #gctime()} of the
|
100
|
* time interval since this method has been called the last time and now.
|
101
|
*
|
102
|
* @return
|
103
|
*/
|
104
|
public long getGCtimeSiceLastCheck() {
|
105
|
long gcTimeNow = gcTime();
|
106
|
long gcTimeSince = gcTimeNow - gcTimeLast;
|
107
|
gcTimeLast = gcTimeNow;
|
108
|
lastCheckTime = System.currentTimeMillis();
|
109
|
return gcTimeSince;
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Returns the time spend in gc as proportion (0.0 to 1.0) of the
|
114
|
* time interval since this method has been called the last time and now.
|
115
|
*
|
116
|
* @return
|
117
|
*/
|
118
|
public double getGCRateSiceLastCheck() {
|
119
|
|
120
|
long gcTimeSince = getGCtimeSiceLastCheck();
|
121
|
long timeDiff = System.currentTimeMillis() - lastCheckTime;
|
122
|
double gcRate = gcTimeSince / (double) timeDiff;
|
123
|
return gcRate;
|
124
|
}
|
125
|
|
126
|
|
127
|
|
128
|
|
129
|
}
|