Java Memory Usage getMemUsage()

Here you can find the source of getMemUsage()

Description

get Mem Usage

License

Open Source License

Declaration

public static String getMemUsage() 

Method Source Code

//package com.java2s;
/**//  w ww.  j  a  va 2 s  .  com
 * AC - A source-code copy detector
 *
 *     For more information please visit:  http://github.com/manuel-freire/ac
 *
 * ****************************************************************************
 *
 * This file is part of AC, version 2.0
 *
 * AC is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * AC 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with AC.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    private static MemoryMXBean mbean = null;

    public static String getMemUsage() {

        if (mbean == null) {
            mbean = ManagementFactory.getMemoryMXBean();
        }

        long used = mbean.getHeapMemoryUsage().getUsed();
        long max = mbean.getHeapMemoryUsage().getMax();
        NumberFormat f = DecimalFormat.getInstance();
        f.setMaximumFractionDigits(2);
        f.setMinimumFractionDigits(2);

        return f.format((double) used * 100.0 / max) + "% : "
                + (used / 1024) + "k / " + (max / 1024) + "k";
    }
}

Related

  1. getMemoryUsage()
  2. getMemoryUsage()
  3. getMemoryUsageSimpleHr()
  4. getMemoryUsageUnix()
  5. getMemoryUtiliztaion()
  6. getPercentMemoryUsage()
  7. getStringMemoryUsage(String s)
  8. getUsageMemory()
  9. memoryUsage()