Project

General

Profile

Download (2.65 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
    public static final Logger logger = Logger.getLogger(JvmManager.class);
26

    
27
    // Java > 8
28
    private static final String SUFFIX_META = "Metaspace";
29

    
30
    public static MemoryUsage getMetaSpaceUsage(){
31
        return getMemoryPoolUsage(SUFFIX_META);
32
    }
33

    
34
    protected static MemoryUsage getMemoryPoolUsage(String nameSuffix) {
35
        List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
36

    
37
        for(MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans){
38
            if(memoryPoolMXBean.getName().endsWith(nameSuffix)){
39
                logger.debug(memoryPoolMXBean.getName()
40
                        + ": init= " + memoryPoolMXBean.getUsage().getInit()
41
                        + ", used= "+ memoryPoolMXBean.getUsage().getUsed()
42
                        + ", max= "+ memoryPoolMXBean.getUsage().getMax()
43
                        + ", committed= "+memoryPoolMXBean.getUsage().getCommitted());
44
                return memoryPoolMXBean.getUsage();
45
            }
46
        }
47
        return null;
48
    }
49

    
50
    public static MemoryUsage getHeapMemoryUsage(){
51

    
52
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
53
        if(memoryMXBean != null){
54
            logger.debug("NonHeapMemoryUsage: "+memoryMXBean.getHeapMemoryUsage());
55
            return memoryMXBean.getHeapMemoryUsage();
56
        }
57
        return null;
58
    }
59

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

    
71
        return Runtime.getRuntime().availableProcessors();
72
    }
73

    
74
    public static Integer getJvmVersion() {
75
        return Integer.parseInt(System.getProperty("java.version").split("\\.")[1]);
76
        // alternatively System.getProperty("java.specification.version");
77
    }
78

    
79
}
(4-4/6)