Home arrow static arrow Java Programming [Archive] - getting memory & processing usage
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - getting memory & processing usage
This topic has 2 replies on 1 page.

Posts:1
Registered: 8/6/04
getting memory & processing usage  
Aug 6, 2004 11:21 AM



 
i am working on a project and in some part of this project i need to check the available memory and processing power of the system. Does any one have an idea?
 

Posts:3,258
Registered: 00-08-28
Re: getting memory & processing usage  
Aug 6, 2004 11:39 AM (reply 1 of 2)



 
http://tutorials.beginners.co.uk/integrate_read/i/216/vs_p/p/p/2
http://forum.java.sun.com/thread.jsp?forum=426&thread=502816&start=15&range=15&hilite=false&q=

Also you couold get size of an object in memory as follows

/*** Measures the approximate size of an object in memory, given a Class which* has a no-argument constructor.*/public final class ObjectSizer {   /**  * First and only argument is the package-qualified name of a class  * which has a no-argument constructor.  */  public static void main(String[] aArguments){    Class theClass = null;    try {      theClass = Class.forName(aArguments[0]);    }    catch (Exception ex) {      System.err.println("Cannot build a Class object: " + aArguments[0]);      System.err.println("Use a package-qualified name, and check classpath.");    }    long size = ObjectSizer.getObjectSize( theClass );    System.out.println("Approximate size of " + theClass + " objects :" + size);  }    /**  * Return the approximate size in bytes, and return zero if the class  * has no default constructor.  *  * @param aClass refers to a class which has a no-argument constructor.  */  public static long getObjectSize( Class aClass ){    long result = 0;     //if the class does not have a no-argument constructor, then    //inform the user and return 0.    try {      aClass.getConstructor( new Class[]{} );    }    catch ( NoSuchMethodException ex ) {      System.err.println(aClass + " does not have a no-argument constructor.");      return result;    }     //this array will simply hold a bunch of references, such that    //the objects cannot be garbage-collected    Object[] objects = new Object[fSAMPLE_SIZE];     //build a bunch of identical objects    try {      Object throwAway = aClass.newInstance();       long startMemoryUse = getMemoryUse();      for (int idx=0; idx < objects.length ; ++idx) {        objects[idx] = aClass.newInstance();      }      long endMemoryUse = getMemoryUse();       float approximateSize = ( endMemoryUse - startMemoryUse ) /100f;      result = Math.round( approximateSize );    }    catch (Exception ex) {      System.err.println("Cannot create object using " + aClass);    }    return result;  }   /// PRIVATE ////  private static int fSAMPLE_SIZE = 100;  private static long fSLEEP_INTERVAL = 100;   private static long getMemoryUse(){    putOutTheGarbage();    long totalMemory = Runtime.getRuntime().totalMemory();     putOutTheGarbage();    long freeMemory = Runtime.getRuntime().freeMemory();     return (totalMemory - freeMemory);  }   private static void putOutTheGarbage() {    collectGarbage();    collectGarbage();  }   private static void collectGarbage() {    try {      System.gc();      Thread.currentThread().sleep(fSLEEP_INTERVAL);      System.runFinalization();      Thread.currentThread().sleep(fSLEEP_INTERVAL);    }    catch (InterruptedException ex){      ex.printStackTrace();    }  }}    ObjectSizer can be easily placed in an interactive console application.  An example run gives :  <div class="jive-quote">java -cp . Console ObjectSizeInterpreter </div>Please enter a class name>java.blah Invalid.  Example:"java.lang.String">java.util.ArrayList Approximate size of class java.util.ArrayList objects in bytes: 80 Please enter a class name>java.util.Vector Approximate size of class java.util.Vector objects in bytes: 80 Please enter a class name>java.util.HashMap Approximate size of class java.util.HashMap objects in bytes: 104 Please enter a class name>java.lang.Long class java.lang.Long does not have a no-argument constructor. Please enter a class name>exit  Bye.  Here is the Interpreter class :   import java.util.*;import java.text.MessageFormat; /*** Given a package-qualified class name, return the approximate size of* the object in bytes.*/public final class ObjectSizeInterpreter implements Interpreter {   /**  * @param aLine is a non-null, package-qualified name of a class.  * @param aResult is a non-null, empty List which acts as an "out"  * parameter; when returned, aResult must contain a non-null, non-empty  * List containing a description of the size of the object.  *  * @return true only if the user has requested to quit the Interpreter.  * @exception IllegalArgumentException if a param does not comply.  */  public boolean parseInput (String  aLine, final List aResult) {    if ( aResult == null ) {      throw new IllegalArgumentException("Result param cannot be null.");    }    if ( !aResult.isEmpty() ){      throw new IllegalArgumentException("Result param must be empty.");    }    if ( aLine == null ) {      throw new IllegalArgumentException("Line must not be null.");    }     boolean hasRequestedQuit = aLine.trim().equalsIgnoreCase(fQUIT) ||                               aLine.trim().equalsIgnoreCase(fEXIT);    if ( hasRequestedQuit ) {      //display only a blank line      aResult.add(fNEW_LINE);    }    else {      try {        Class theClass = Class.forName(aLine);        long size = ObjectSizer.getObjectSize( theClass );        if ( size > 0 ){          Object[] insertedData = {theClass, new Long(size) };          MessageFormat sizeMessage = new MessageFormat(fPATTERN);          String message = sizeMessage.format( insertedData );          aResult.add( message );          aResult.add( fNEW_LINE );        }        aResult.add( fDEFAULT_PROMPT );      }      catch (ClassNotFoundException ex){        //recover by asking the user for corrected input        aResult.clear();        aResult.add(fERROR_PROMPT);      }    }     if ( aResult.isEmpty() ) {      throw new IllegalStateException("Result must be non-empty.");    }    return hasRequestedQuit;  }   /**  * Return the text to be displayed upon start-up of the Interpreter.  */  public String getHelloPrompt() {    return fHELLO_PROMPT;  }   /// PRIVATE /////  private static final String fHELLO_PROMPT = "Please enter a class name>";  private static final String fDEFAULT_PROMPT = "Please enter a class name>";  private static final String fERROR_PROMPT = "Invalid.  Example:\"java.lang.String\">";  private static final String fPATTERN = "Approximate size of {0} objects in bytes: {1}";  private static final String fQUIT = "quit";  private static final String fEXIT = "exit";  private static final String fNEW_LINE = System.getProperty("line.separator");}  
 

Posts:826
Registered: 7/1/03
Re: getting memory & processing usage  
Aug 6, 2004 1:03 PM (reply 2 of 2)



 
I thought JVMTI was supposed to do this stuff (Mind you not looked closely at it yet myself).
/k1
 
This topic has 2 replies on 1 page.