Project

General

Profile

Download (2.49 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.server;
11

    
12
import java.lang.management.ManagementFactory;
13
import java.lang.management.MemoryMXBean;
14
import java.lang.management.MemoryPoolMXBean;
15
import java.lang.management.MemoryUsage;
16
import java.util.List;
17

    
18
import org.apache.log4j.Logger;
19

    
20
/**
21
 * @author a.kohlbecker
22
 * @date 30.07.2010
23
 *
24
 */
25
public class JvmManager {
26

    
27
    /**
28
     *
29
     */
30
    private static final String SUFFIX_PERM_GEN = "Perm Gen";
31

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

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

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

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

    
54
    public static MemoryUsage getHeapMemoryUsage(){
55

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

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

    
75
        return Runtime.getRuntime().availableProcessors();
76
    }
77

    
78
}
(4-4/6)