在Java中-如何检查正在运行的进程是否使用了超过1GB的内存?
我的任务是制作一个程序(用Java编写,因为它是我现在唯一可以编写代码的语言),让看门狗监视系统。如果一个进程使用超过1GB的RAM,系统会踢狗,看门狗会在控制台中打印一条消息(即:“进程-名称-使用了超过1GB的内存”)。然而,我已经做了看门狗……我还没有找到一种方法来从Java系统中获取每个进程的信息 编辑:要更正,我需要Windows 7 64位的帮助。我需要运行游戏或应用程序(Skype等)的过程信息 任何帮助都会非常有用 监督机构:在Java中-如何检查正在运行的进程是否使用了超过1GB的内存?,java,process,watchdog,Java,Process,Watchdog,我的任务是制作一个程序(用Java编写,因为它是我现在唯一可以编写代码的语言),让看门狗监视系统。如果一个进程使用超过1GB的RAM,系统会踢狗,看门狗会在控制台中打印一条消息(即:“进程-名称-使用了超过1GB的内存”)。然而,我已经做了看门狗……我还没有找到一种方法来从Java系统中获取每个进程的信息 编辑:要更正,我需要Windows 7 64位的帮助。我需要运行游戏或应用程序(Skype等)的过程信息 任何帮助都会非常有用 监督机构: import java.util.Enumerati
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();
}
}
}
“每个进程”:操作系统级进程?如何获取这些信息取决于操作系统。我使用的是64位Windows 7。我想查看游戏或Skype等应用程序的进程信息。请查看此(重复)问题及其原始答案,以获取有关如何访问Windows操作系统信息(如正在运行的进程列表及其性能计数器)的链接
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());
}
}
}