简体   繁体   English

使用Java中的ProcessBuilder调用的外部进程使用的内存和占用的时间

[英]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.

相关问题 ProcessBuilder 调用的 Java 进程永远休眠 - Java process invoked by ProcessBuilder sleeps forever Java ProcessBuilder启动的进程的内存消耗 - Memory comsumption of a process initiated by Java ProcessBuilder 使用ProcessBuilder将命令发送到外部进程 - Send a command to external process using ProcessBuilder 如何获取使用ProcessBuilder执行的进程的内存使用量和cpu时间? - How do I get the memory usage and cpu time of a process executed using ProcessBuilder? 从由ProcessBuilder启动的进程中获取内存使用情况和执行时间 - Get memory usage and execution time from a process initiated with the ProcessBuilder 使用 ProcessBuilder 运行外部 Java 进程并给出堆大小 - Running external Java Process with ProcessBuilder and give heap size Java ProcessBuilder内存 - Java ProcessBuilder memory 使用 Java 中的 ProcessBuilder 将进程的输出重定向到另一个进程的输入 - Redirecting the output of a process into the input of another process using ProcessBuilder in Java 哪个内存用于从 java - java 堆空间或操作系统内存运行的外部进程? - Which memory is used for external process run from java - java heap space or OS memory? 如何找到Java处理所需的总时间 - how to find the total time taken for a process in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM