[英]Memory used and Time Taken by external process invoked using ProcessBuilder in Java
I searched stackoverflow for this question but i did not got any useful answer. 我在stackoverflow上搜索了此问题,但没有得到任何有用的答案。 I want to get the memory used and time taken by the external process which in being invoked by the ProcessBuilder in Java. 我想获取外部进程使用的内存和所花费的时间,这些外部进程由Java中的ProcessBuilder调用。 If the process exceeds a time limit/memory limit then i need to kill it. 如果该过程超过了时间限制/内存限制,那么我需要将其杀死。
To calculate the time taken by the process I am using the following code: 为了计算该过程花费的时间,我使用以下代码:
process = new ProcessBuilder(command).start();
starttTime = System.currentTimeMillis();
flag=false;
while (flag != true) {
try {
returnValue = process.exitValue();
flag = true;
if (returnValue != 0) {
System.out.println("Abnormal termination");
} else {
System.out.println("Normal termination");
}
System.out.println("Done");
} catch (IllegalThreadStateException e) {
}
currentTime = System.currentTimeMillis();
if (currentTime - startTime > timeLimitInMilli) {
System.out.println("process exceeding time limit");
break;
}
}
But this method of calculating the time is slow. 但是这种计算时间的方法很慢。 Is there any faster method of doing the same. 有没有更快的方法可以做到这一点。 Also, I am unable to get the memory used by the process. 另外,我无法获取该进程使用的内存。 How can I get it ? 我怎么才能得到它 ?
Also, this code is being used by multiple threads that means more than one process is being invoked at a time by different threads. 同样,该代码被多个线程使用,这意味着不同的线程一次调用多个进程。
One solution would be to run a wrapper script instead of running the command directly. 一种解决方案是运行包装器脚本,而不是直接运行命令。 The purpose of the script would be to check the execution time and memory usage (using OS utilities) and return an error code to indicate any problems. 该脚本的目的是检查执行时间和内存使用情况(使用OS实用程序)并返回错误代码以指示任何问题。
Alternatively, you could use JNI or other OS-specific commands to determine the memory usage of the other process. 另外,您可以使用JNI或其他特定于操作系统的命令来确定其他进程的内存使用情况。
If the other process is a JVM or some other program that will report back its memory usage (for example, via an MXBean) then your loop could ask it directly to report memory usage. 如果另一个进程是将报告其内存使用情况的JVM或其他程序(例如,通过MXBean),则您的循环可以直接要求它报告内存使用情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.