Java Memory Used jvmInUseMemory(String size, Boolean txtByte)

Here you can find the source of jvmInUseMemory(String size, Boolean txtByte)

Description

Obtain JVM's In Use Memory.

License

Open Source License

Parameter

Parameter Description
size null, AUTO, B, KB, MB, GB, TB, or PB (PetaByte does not exist yet) Is not case sensitive.
txtByte true if include byte extension, false exclude extension

Return

bytes size

Declaration

public static String jvmInUseMemory(String size, Boolean txtByte) 

Method Source Code

//package com.java2s;
/*//from   www  .ja va 2 s.c om
 * 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.
 */

public class Main {
    /**
     * Obtain JVM's In Use 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 jvmInUseMemory(String size, Boolean txtByte) {
        return convertByteSize(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(), 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

  1. getUsedMemoryInMb()
  2. getUsedMemoryInMegabytes()
  3. getUsedMemoryMBs()
  4. getUsedMemoryMegaBytes()
  5. getUsedMemoryStr()
  6. makeUsedPhysicalMemoryData(long systemPhysicalMaxValue, long systemPhysicalFreeValue)
  7. measureMemoryUse()
  8. memoryUsed()
  9. memoryUsed()