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
public class JvmManager {
24

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

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

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

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

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

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

    
56
    public static MemoryUsage getHeapMemoryUsage(){
57

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

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

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

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

    
85
}
(4-4/6)