user2109489
user2109489

Reputation: 1

how to find available system memory, disk space, cpu usage using system calls from java to windows OS

how to find available system memory, disk space, cpu usage of windows OS using system calls from java?

*in windows operating system

wat i actually want to do is ,,, accept a file from client user and save the file in the server after checking the available space. my server and client are connected using a java tcp socket program!

Upvotes: 0

Views: 2477

Answers (1)

Joe2013
Joe2013

Reputation: 997

You can get some limited information using the below program. This was already answered by some one else in the same forum and I am just reproducing the same.

import java.io.File;

public class MemoryInfo {
  public static void main(String[] args) {
    /* Total number of processors or cores available to the JVM */
    System.out.println("Available processors (cores): " + 
        Runtime.getRuntime().availableProcessors());

    /* Total amount of free memory available to the JVM */
    System.out.println("Free memory (bytes): " + 
        Runtime.getRuntime().freeMemory());

    /* This will return Long.MAX_VALUE if there is no preset limit */
    long maxMemory = Runtime.getRuntime().maxMemory();
    /* Maximum amount of memory the JVM will attempt to use */
    System.out.println("Maximum memory (bytes): " + 
        (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));

    /* Total memory currently in use by the JVM */
    System.out.println("Total memory (bytes): " + 
        Runtime.getRuntime().totalMemory());

    /* 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 (bytes): " + root.getTotalSpace());
      System.out.println("Free space (bytes): " + root.getFreeSpace());
      System.out.println("Usable space (bytes): " + root.getUsableSpace());
    }
  }
}

Upvotes: 1

Related Questions