1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package utils;
24
25import java.lang.management.ManagementFactory;
26import java.lang.management.MemoryPoolMXBean;
27
28/**
29 * Utility to obtain memory pools statistics
30 *
31 */
32public class Pools {
33
34    private static final String EDEN_SPACE_POOL = "Eden Space";
35    private static final String OLD_GEN_POOL = "Old Gen";
36    private static final String METASPACE_POOL = "Metaspace";
37    private static final String SURVIVOR_SPACE = "Survivor Space";
38
39    public static long getNGMaxSize() {
40        // NewGen is consists of Eden and two Survivor spaces
41        return getPoolMaxSize(EDEN_SPACE_POOL) + 2 * getPoolMaxSize(SURVIVOR_SPACE);
42    }
43
44    public static long getHeapCommittedSize() {
45        return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getCommitted() / 1024;
46    }
47
48    public static long getEdenCommittedSize() {
49        return getPoolCommittedSize(EDEN_SPACE_POOL);
50    }
51
52    public static long getOldGenCommittedSize() {
53        return getPoolCommittedSize(OLD_GEN_POOL);
54    }
55
56    public static long getMetaspaceCommittedSize() {
57        return getPoolCommittedSize(METASPACE_POOL);
58    }
59
60    private static long getPoolMaxSize(String poolName) {
61        long result;
62        MemoryPoolMXBean pool = findPool(poolName);
63        if (pool != null) {
64            if (pool.getUsage().getMax() == -1) {
65                result = -1;
66            } else {
67                result = pool.getUsage().getCommitted() / 1024;
68            }
69        } else {
70            throw new RuntimeException("Pool '" + poolName + "' wasn't found");
71        }
72        log("Max size of the pool '" + poolName + "' is " + result);
73        return result;
74    }
75
76    private static long getPoolCommittedSize(String poolName) {
77        long result;
78        MemoryPoolMXBean pool = findPool(poolName);
79        if (pool != null) {
80            if (pool.getUsage().getCommitted() == -1) {
81                result = -1;
82            } else {
83                result = pool.getUsage().getCommitted() / 1024;
84            }
85        } else {
86            throw new RuntimeException("Pool '" + poolName + "' wasn't found");
87        }
88        log("Committed size of the pool '" + poolName + "' is " + result);
89        return result;
90    }
91
92    private static MemoryPoolMXBean findPool(String poolName) {
93        for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
94            if (pool.getName().contains(poolName)) {
95                return pool;
96            }
97        }
98        return null;
99    }
100
101    private static void log(String s) {
102        System.out.println(s);
103    }
104
105}
106