Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-18-2007, 03:27 PM
Member
 
Join Date: Jul 2007
Posts: 17
sathish_2111 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-18-2007, 06:08 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-19-2007, 05:29 PM
Member
 
Join Date: Jul 2007
Posts: 1
mir2ming is on a distinguished road
i give a demo to you:

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts free book rajendar Web Frameworks 0 11-27-2007 08:51 AM
Free WebServices javaplus Web Frameworks 4 11-26-2007 12:00 PM
Is there a free scripting tool BMD Java Tips 1 11-07-2007 09:49 AM
Free Java Lectures curmudgeon99 Java Books / Tutorials / Tips 1 07-26-2007 03:47 PM
Free eBooks- Java and other technology derrickD Reviews / Advertising 0 05-18-2007 07:49 PM


All times are GMT +3. The time now is 11:09 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org