Project

General

Profile

Download (3.82 KB) Statistics
| Branch: | Tag: | Revision:
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
 */
24
public class JvmMonitor {
25

    
26

    
27
    public static final Logger logger = Logger.getLogger(JvmMonitor.class);
28

    
29
    private long gcTimeLast = 0;
30

    
31
    private long lastCheckTime = 0;
32

    
33
    /**
34
     * Returns the sum of approximate accumulated collection elapsed time in milliseconds
35
     * as reported by all garbage collectors.
36
     *
37
     * This method returns -1 if the collection elapsed time is undefined.
38
     *
39
     * @return
40
     */
41
    public long gcTime() {
42
        List<GarbageCollectorMXBean> gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
43

    
44
        //logger.setLevel(Level.DEBUG);
45

    
46
        long gcTimeSum = -1;
47
        long collectorGcTime;
48
        for(GarbageCollectorMXBean gcMXBean : gcMXBeans){
49
                if(gcTimeSum == -1) {
50
                    gcTimeSum = 0;
51
                }
52
                collectorGcTime = gcMXBean.getCollectionTime();
53
                logger.debug("cgMxBean: " + gcMXBean.getName()
54
                        + " gcTime = " + collectorGcTime
55
                        + " gcCount = " + gcMXBean.getCollectionCount());
56
                if(collectorGcTime != -1) {
57
                    // only sum up if the time is defined
58
                    gcTimeSum += gcMXBean.getCollectionTime();
59
                }
60
        }
61
        logger.debug("gcTimeSum = " + gcTimeSum);
62
        return gcTimeSum;
63

    
64
    }
65

    
66
    public MemoryUsage getHeapMemoryUsage(){
67

    
68
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
69
        if(memoryMXBean != null){
70
            logger.debug("HeapMemoryUsage: " + memoryMXBean.getHeapMemoryUsage());
71
            return memoryMXBean.getHeapMemoryUsage();
72
        }
73
        return null;
74
    }
75

    
76

    
77
    public boolean hasFreeHeap(long freeHeapLimit) {
78

    
79
        if(!_hasFreeHeap(freeHeapLimit)) {
80
            Runtime.getRuntime().gc();
81
            return _hasFreeHeap(freeHeapLimit);
82
        }
83
        return true;
84
    }
85

    
86
    /**
87
     * @param maxUsedFraction
88
     * @return
89
     */
90
    private boolean _hasFreeHeap(long freeHeapLimit) {
91
        long freeHeap = getFreeHeap(false);
92
        return freeHeap > freeHeapLimit;
93
    }
94

    
95
    /**
96
     * @return
97
     */
98
    public long getFreeHeap(boolean gcBeforeMeasure) {
99
        if(gcBeforeMeasure) {
100
            Runtime.getRuntime().gc();
101
        }
102
        MemoryUsage heapUsage = getHeapMemoryUsage();
103
        long freeHeap =  heapUsage.getMax() - heapUsage.getUsed();
104
        return freeHeap;
105
    }
106

    
107
    /**
108
     * Returns the gcTime in milliseconds as obtained through {@link #gctime()} of the
109
     * time interval since this method has been called the last time and now.
110
     *
111
     * @return
112
     */
113
    public long getGCtimeSiceLastCheck() {
114
        long gcTimeNow = gcTime();
115
        long gcTimeSince = gcTimeNow - gcTimeLast;
116
        gcTimeLast = gcTimeNow;
117
        lastCheckTime  = System.currentTimeMillis();
118
        return gcTimeSince;
119
    }
120

    
121
    /**
122
     * Returns the time spend in gc as proportion (0.0 to 1.0) of the
123
     * time interval since this method has been called the last time and now.
124
     *
125
     * @return
126
     */
127
    public double getGCRateSiceLastCheck() {
128

    
129
        long gcTimeSince = getGCtimeSiceLastCheck();
130
        long timeDiff = System.currentTimeMillis() - lastCheckTime;
131
        double gcRate = gcTimeSince / (double) timeDiff;
132
        return gcRate;
133
    }
134

    
135

    
136

    
137

    
138
}
(13-13/23)