Results 1 to 3 of 3
  1. #1
    sathish_2111 is offline Member
    Join Date
    Jul 2007
    Posts
    17
    Rep Power
    0

    Default How Can I get free memory ?

    Hi

    I have two question...

    How can I get the free memory space of every hard drives in the machine?

    How can I get the amount memory used by a particular process?

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    I don't think there are is feasible ways of doing it with pure Java. I think you will need to write them using JNI.

  3. #3
    mir2ming is offline Member
    Join Date
    Jul 2007
    Posts
    1
    Rep Power
    0

    Default

    i give a demo to you:

    Java Code:
    import Java.io.BufferedReader;
    import Java.io.InputStreamReader;
    
    /**
    * Determine free disk space for a given directory by 
    * parsing the output of the dir command.
    * This class is inspired by the code at
    * Works only under Windows under certain circumstances.
    * Yes, it's that shaky.
    * Requires Java 1.4 or higher.
    * @[EMAIL PROTECTED] 
    */
    public class Diskspace
    {
    private Diskspace()
    {
    // prevent instantiation of this class
    }
    
    /**
    * Return available free disk space for a directory. 字串8 
    * @[EMAIL PROTECTED]
    dirName name of the directory
    * @[EMAIL PROTECTED]
    free disk space in bytes or -1 if unknown
    */
    public static long getFreeDiskSpace(String dirName)
    {
    try
    {
    // guess correct 'dir' command by looking at the 
    // operating system name
    String os = System.getProperty("os.name");
    String command;
    if (os.equals("Windows NT") ||
    os.equals("Windows 2000"))
    {
    command = "cmd.exe /c dir " dirName;
    }
    else
    {
    command = "command.com /c dir " dirName;
    }
    // run the dir command on the argument directory name
    Runtime runtime = Runtime.getRuntime();
    Process process = null;
    process = runtime.exec(command);
    if (process == null)
    {
    return -1;
    }
    // read the output of the dir command
    // only the last line is of interest 字串1 
    BufferedReader in = new BufferedReader(
    new InputStreamReader(process.getInputStream()));
    String line;
    String freeSpace = null;
    while ((line = in.readLine()) != null)
    {
    freeSpace = line;
    }
    if (freeSpace == null)
    {
    return -1;
    }
    process.destroy();
    // remove dots & commas & leading and trailing whitespace
    freeSpace = freeSpace.trim();
    freeSpace = freeSpace.replaceAll("\\.", "");
    freeSpace = freeSpace.replaceAll(",", "");
    String[] items = freeSpace.split(" ");
    // the first valid numeric value in items after(!) index 0
    // is probably the free disk space
    int index = 1;
    while (index < items.length)
    {
    try
    {
    long bytes = Long.parseLong(items[index ]);
    return bytes;
    }
    catch (NumberFormatException nfe)
    {
    }
    }
    return -1; 字串3 
    }
    catch (Exception exception)
    {
    return -1;
    } 
    }
    
    /**
    * Command line program to print the free diskspace to stdout
    * for all 26 potential root directories A:\ to Z:\
    * (when no parameters are given to this program)
    * or for those directories (drives) specified as parameters.
    * @[EMAIL PROTECTED]
    args program parameters
    */
    public static void main(String[] args)
    {
    if (args.length == 0)
    {
    for (char c = 'A'; c <= 'Z'; c )
    {
    String dirName = c ":\\";
    System.out.println(dirName " " 
    getFreeDiskSpace(dirName));
    }
    }
    else
    {
    for (int i = 0; i < args.length; i )
    {
    System.out.println(args " " 
    getFreeDiskSpace(args));
    }
    }
    }
    }
    Last edited by JavaBean; 07-19-2007 at 05:36 PM. Reason: Code placed inside [code] tags.

Similar Threads

  1. Struts free book
    By rajendar in forum Web Frameworks
    Replies: 0
    Last Post: 11-27-2007, 08:51 AM
  2. Free WebServices
    By javaplus in forum Web Frameworks
    Replies: 4
    Last Post: 11-26-2007, 12:00 PM
  3. Is there a free scripting tool
    By BMD in forum Java Tip
    Replies: 1
    Last Post: 11-07-2007, 09:49 AM
  4. Free eBooks- Java and other technology
    By derrickD in forum Reviews / Advertising
    Replies: 0
    Last Post: 05-18-2007, 07:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •