2

I am tasked with making a program (In Java, since its the only language I can code in right now) where a watchdog monitors the system. If one process uses more than 1GB of RAM, the system kicks the dog and the watchdog prints a message in the console (I.E.: "Process -name- used more than 1 GB of memory"). I've made the Watchdog, however...I have yet to come across a way to get every process' information from the system in Java.

EDIT: To rectify, I need help on Windows 7 64bit. I require the process info of let's say running games or application (Skype, etc).

Any help would be tremendously useful!

Watchdog:

import java.util.Enumeration;
import java.util.Vector;

public class Watchdog implements Runnable {

    private Vector observers = new Vector(1);

    private final long timeout;

    private boolean stopped = false;

    public Watchdog(final long timeout) {
        if (timeout < 1) {
            throw new IllegalArgumentException("timeout must not be less than 1.");
        }
        this.timeout = timeout;
    }

    public void addTimeoutObserver(final TimeoutObserver to) {
        observers.addElement(to);
    }

    public void removeTimeoutObserver(final TimeoutObserver to) {
        observers.removeElement(to);
    }

    protected final void fireTimeoutOccured() {
        Enumeration e = observers.elements();
        while (e.hasMoreElements()) {
            ((TimeoutObserver) e.nextElement()).timeoutOccured(this);
        }
    }

    public synchronized void start() {
        stopped = false;
        Thread t = new Thread(this, "WATCHDOG");
        t.setDaemon(true);
        t.start();
    }

    public synchronized void stop() {
        stopped = true;
        notifyAll();
    }

    public synchronized void run() {
        final long until = System.currentTimeMillis() + timeout;
        long now;
        while (!stopped && until > (now = System.currentTimeMillis())) {
            try {
                wait(until - now);
            } catch (InterruptedException e) {
            }
        }
        if (!stopped) {
            fireTimeoutOccured();
        }
    }

}

Main class:

 import java.io.File;

 public class Main {
  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 available to the JVM */
    System.out.println("Total memory available to JVM (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());
    }


  }
}
  • "every process": operating system level process? How to obtain this information depends on the operating system. – laune Jun 15 '14 at 07:13
  • I'm on Windows 7 64bit. I want to check the process info of lets say a game or an application like Skype. – Valentin V. Jun 15 '14 at 07:17
  • check this (duplicate) question http://stackoverflow.com/questions/23222251/how-to-get-windows-load-values and its original answers for links on how to access Windows OS information like list of running processes and their performance counters – xmojmr Jul 14 '14 at 12:31

0 Answers0