ome.services.util.JvmSettingsCheck.java Source code

Java tutorial

Introduction

Here is the source code for ome.services.util.JvmSettingsCheck.java

Source

/*
 * Copyright (C) 2013 University of Dundee & Open Microscopy Environment.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package ome.services.util;

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Hook run by the context which prints out JVM-related
 * information, primarily Java version, max heap size
 * and available processors.
 *
 * @author Josh Moore, josh at glencoesoftware.com
 */
public class JvmSettingsCheck {

    public final static Log log = LogFactory.getLog(JvmSettingsCheck.class);

    public JvmSettingsCheck() {
        final String fmt = "%s = %6s";
        final Runtime rt = Runtime.getRuntime();
        final int mb = 1024 * 1024;

        StringBuilder version = new StringBuilder();
        for (String key : new String[] { "java.version", "os.name", "os.arch", "os.version" }) {
            if (version.length() != 0) {
                version.append("; ");
            }
            version.append(System.getProperty(key));
        }

        log.info("Java version: " + version);
        log.info(String.format(fmt, "Max Memory (MB):  ", (rt.maxMemory() / mb)));
        log.info(String.format(fmt, "OS Memory (MB):   ", (getPhysicalMemory() / mb)));
        log.info(String.format(fmt, "Processors:       ", rt.availableProcessors()));
    }

    public static long getPhysicalMemory() {
        try {
            MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
            Object attribute = mBeanServer.getAttribute(new ObjectName("java.lang", "type", "OperatingSystem"),
                    "TotalPhysicalMemorySize");
            return Long.valueOf(attribute.toString());
        } catch (Exception e) {
            log.debug("Failed to get physical memory", e);
            return -1;
        }
    }

    public static void main(String[] args) {
        new JvmSettingsCheck();
    }

}