Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Getting OS-level system information using Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 978
    Comment on it

    Hello readers,

    Here below is a small Java program to extract information such as the current disk space used, CPU utilisation and memory used in the underlying OS. To get information related to the disk usage you can also use java.io.File class, for this you need to have Java 1.6 or higher.

     

    Here is our class GetInfo.java which contains our main method.

    
    import java.io.File;
    
    public class GetInfo {
    	public static void main(String[] args) {
    		/* Number of processors or cores available to the JVM */
    		System.out.println("Available processors (cores): "
    				+ Runtime.getRuntime().availableProcessors());
    		/* Amount of free memory available to the JVM */
    		System.out.println("Free memory (Mega Bytes): "
    				+ Runtime.getRuntime().freeMemory() / (1024 * 1024));
    		/* This will return Long.MAX_VALUE if there is no preset limit */
    		long maxMemory = Runtime.getRuntime().maxMemory() / (1024 * 1024);
    		/* Maximum amount of memory the JVM will attempt to use */
    		System.out.println("Maximum memory (Mega Bytes): "
    				+ (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));
    		/* Total memory available to the JVM */
    		System.out.println("Total memory available to JVM (Mega Bytes): "
    				+ Runtime.getRuntime().totalMemory() / (1024 * 1024));
    
    		/* Get a list of all filesystem roots on this system */
    		File[] roots = File.listRoots();
    		/* For each filesystem root, print some info */
    		for (File root : roots) {
    			System.out.println("File system root: " + root.getAbsolutePath());
    			System.out.println("Total space (Mega Bytes): "
    					+ root.getTotalSpace() / (1024 * 1024));
    			System.out.println("Free space (Mega Bytes): "
    					+ root.getFreeSpace() / (1024 * 1024));
    			System.out.println("Usable space (Mega Bytes): "
    					+ root.getUsableSpace() / (1024 * 1024));
    		}
    	}
    
    }
    

    Output the above program would be like

    Available processors (cores): 4
    Free memory (Mega Bytes): 56
    Maximum memory (Mega Bytes): 855
    Total memory available to JVM (Mega Bytes): 57
    File system root: /
    Total space (Mega Bytes): 93740
    Free space (Mega Bytes): 61103
    Usable space (Mega Bytes): 56319
    
    

    thanks for reading :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: