Get memory information : Runtime « Development Class « Java






Get memory information

      
/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 *  aion-unique 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique 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 aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author lord_rex This class is for get/log system informations.
 * 
 */ 
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Util{

  public static String[] getMemoryInfo()
  {
    double max = Runtime.getRuntime().maxMemory() / 1024; // maxMemory is the upper limit the jvm can use
    double allocated = Runtime.getRuntime().totalMemory() / 1024; // totalMemory the size of the current allocation
                                    // pool
    double nonAllocated = max - allocated; // non allocated memory till jvm limit
    double cached = Runtime.getRuntime().freeMemory() / 1024; // freeMemory the unused memory in the allocation pool
    double used = allocated - cached; // really used memory
    double useable = max - used; // allocated, but non-used and non-allocated memory
    DecimalFormat df = new DecimalFormat(" (0.0000'%')");
    DecimalFormat df2 = new DecimalFormat(" # 'KB'");
    return new String[]
    { //
    "+----", //
        "| Global Memory Informations at " + getRealTime().toString() + ":", //
        "|    |", //
        "| Allowed Memory:" + df2.format(max), //
        "|    |= Allocated Memory:" + df2.format(allocated) + df.format(allocated / max * 100), //
        "|    |= Non-Allocated Memory:" + df2.format(nonAllocated) + df.format(nonAllocated / max * 100), //
        "| Allocated Memory:" + df2.format(allocated), //
        "|    |= Used Memory:" + df2.format(used) + df.format(used / max * 100), //
        "|    |= Unused (cached) Memory:" + df2.format(cached) + df.format(cached / max * 100), //
        "| Useable Memory:" + df2.format(useable) + df.format(useable / max * 100), //
        "+----" //
    };
  }
  public static String getRealTime()
  {
    SimpleDateFormat String = new SimpleDateFormat("H:mm:ss");
    return String.format(new Date());
  }

}

   
    
    
    
    
    
  








Related examples in the same category

1.Runtime.getRuntime().exec
2.Get Number of Available Processors
3.Execute system command
4.Determine when the application is about to exit
5.Getting the Size of the Java Memory Heap
6.Read all information that the child process sends to its standard output stream
7.Execute a command from code
8.Execute a command with more than one argument
9.Launch a Unix script with Java
10.Read output from a Command execution
11.Send an Input to a Command
12.From Runtime.exec() to ProcessBuilder
13.Get current size of heap in bytes
14.Get maximum size of heap in bytes.
15.Get amount of free memory within the heap in bytes.
16.Minimize all programs on Windows to show the Desktop
17.Command and its arguments supplied in an array
18.Execute a command with an argument that contains a space
19.Execute a command with an argument that contains a space: use array
20.Calculate process elapsed time
21.Registering Shutdown Hooks for Virtual Machine
22.Returns a description of the JVM.
23.Returns a description of the operating system and processor configuration.
24.Returns a report of used and available memory.
25.Ensure that there is only one instance
26.Returns used(max) memory in MB