Project

General

Profile

Download (2.87 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.server;
10

    
11
import java.lang.management.ManagementFactory;
12
import java.lang.management.MemoryMXBean;
13
import java.lang.management.MemoryPoolMXBean;
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
 * @date 30.07.2010
22
 *
23
 */
24
public class JvmManager {
25

    
26
    // Java 7
27
    private static final String SUFFIX_PERM_GEN = "Perm Gen";
28
    // Java > 8
29
    private static final String SUFFIX_META = "Metaspace";
30

    
31
    public static final Logger logger = Logger.getLogger(JvmManager.class);
32

    
33
    public static MemoryUsage getPermGenSpaceUsage(){
34
        return getMemoryPoolUsage(SUFFIX_PERM_GEN);
35
    }
36

    
37
    public static MemoryUsage getMetaSpaceUsage(){
38
        return getMemoryPoolUsage(SUFFIX_META);
39
    }
40

    
41
    protected static MemoryUsage getMemoryPoolUsage(String nameSuffix) {
42
        List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
43

    
44
        for(MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans){
45
            if(memoryPoolMXBean.getName().endsWith(nameSuffix)){
46
                logger.debug(memoryPoolMXBean.getName()
47
                        + ": init= " + memoryPoolMXBean.getUsage().getInit()
48
                        + ", used= "+ memoryPoolMXBean.getUsage().getUsed()
49
                        + ", max= "+ memoryPoolMXBean.getUsage().getMax()
50
                        + ", committed= "+memoryPoolMXBean.getUsage().getCommitted());
51
                return memoryPoolMXBean.getUsage();
52
            }
53
        }
54
        return null;
55
    }
56

    
57
    public static MemoryUsage getHeapMemoryUsage(){
58

    
59
            MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
60
            if(memoryMXBean != null){
61
                logger.debug("NonHeapMemoryUsage: "+memoryMXBean.getHeapMemoryUsage());
62
                return memoryMXBean.getHeapMemoryUsage();
63
            }
64
            return null;
65
        }
66

    
67
    /**
68
     *
69
     * Note, however, that this does not distinguish virtual cores from physical cores. so, for example,
70
     * if your machine has hyperthreading enabled you will see double the number of physical cores.
71
     *
72
     * see https://community.oracle.com/thread/2141632?start=0&tstart=0
73
     *
74
     * @return
75
     */
76
    public static int availableProcessors() {
77

    
78
        return Runtime.getRuntime().availableProcessors();
79
    }
80

    
81
    public static Integer getJvmVersion() {
82
        return Integer.parseInt(System.getProperty("java.version").split("\\.")[1]);
83
        // alternatively System.getProperty("java.specification.version");
84
    }
85

    
86
}
(4-4/6)