Obtain JVM's Total Memory. - Java Java Virtual Machine

Java examples for Java Virtual Machine:Memory

Description

Obtain JVM's Total Memory.

Demo Code

/*/*w  ww  .j av a 2s. c  o  m*/
 * RED5 Open Source Flash Server - http://code.google.com/p/red5/
 * 
 * Copyright 2006-2012 by respective authors (see below). All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

public class Main {
    /**
     * Obtain JVM's Total Memory.
     * 
     * @param size null, AUTO, B, KB, MB, GB, TB, or PB
     * (PetaByte does not exist yet)
     * Is not case sensitive.
     * @param txtByte true if include byte extension, false exclude extension
     * @return bytes size
     * 
     */
    public static String jvmTotalMemory(String size, Boolean txtByte) {
        return convertByteSize(Runtime.getRuntime().totalMemory(), size,
                txtByte);
    }

    /**
     * Permit to convert bytes to ALMOST any upper bytes with/without extension
     * (Currently at existing TeraByte but one step ahead, PetaByte)
     * 
     * @param bytes length of bytes
     * @param size null, AUTO, B, KB, MB, GB, TB, or PB
     * (PetaByte does not exist yet)
     * Is not case sensitive.
     * @param txtByte true if include byte extension, false exclude extension
     * @return bytes size
     */
    public static String convertByteSize(Long bytes, String size,
            Boolean txtByte) {
        String convertB = null;
        if (bytes != null) {
            if (size != null)
                size = size.toUpperCase();
            if (txtByte == null)
                txtByte = true;
            Long num = 1024L;//DO NOT CHANGE THIS VALUE!
            if (size == null || size.equals("AUTO")) {
                if (bytes > (num * num * num * num * num)) {
                    convertB = bytes / (num * num * num * num * num) + "";
                    size = "PB";
                } else if (bytes > (num * num * num * num)) {
                    convertB = bytes / (num * num * num * num) + "";
                    size = "TB";
                } else if (bytes > (num * num * num)) {
                    convertB = bytes / (num * num * num) + "";
                    size = "GB";
                } else if (bytes > (num * num)) {
                    convertB = bytes / (num * num) + "";
                    size = "MB";
                } else if (bytes > num) {
                    convertB = bytes / num + "";
                    size = "KB";
                } else {
                    convertB = bytes + "";
                    size = "B";
                }
            } else if (size.equals("PB")) {
                convertB = bytes / (num * num * num * num * num) + "";
            } else if (size.equals("TB")) {
                convertB = bytes / (num * num * num * num) + "";
            } else if (size.equals("GB")) {
                convertB = bytes / (num * num * num) + "";
            } else if (size.equals("MB")) {
                convertB = bytes / (num * num) + "";
            } else if (size.equals("KB")) {
                convertB = bytes / num + "";
            } else {
                convertB = bytes + "";
            }
            if (txtByte) {
                if (size.equals("PB")) {
                    convertB += "PB";
                } else if (size.equals("TB")) {
                    convertB += "TB";
                } else if (size.equals("GB")) {
                    convertB += "GB";
                } else if (size.equals("MB")) {
                    convertB += "MB";
                } else if (size.equals("KB")) {
                    convertB += "KB";
                } else {
                    convertB += "B";
                }
            }
        }
        return convertB;
    }
}

Related Tutorials