Get memory information : Runtime « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Runtime 




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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.