0

我想使用 java 查询 MySQL、MongoDB 和 Neo4j 目前使用的内存。我已经搜索过它,但没有找到任何相关的东西。我的操作系统是 Windows,我正在使用 Eclipse。谢谢

4

2 回答 2

0

我刚刚想出了一个 java 代码模板来运行一个 windows 命令,你需要运行tasklist命令来获得所有进程表以及所有信息,包括每个进程使用的内存。

这是代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TaskMemory {

    public static void main(String[] args) {
        Process process = null;
        if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0) {
            String command = "cmd.exe /c tasklist";
            try {
                process = Runtime.getRuntime().exec(command);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (process != null) {
            InputStream good = process.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(good));
            StringBuffer sb = new StringBuffer();
            String str;

            try {
                for(int i = 0; i < 3; i++){
                    if((str = bufferedReader.readLine()) != null){
                        sb.append(str).append("\n");
                    }
                }
                while ((str = bufferedReader.readLine()) != null) {
                    if(str.matches("(.*)mysqld.exe(.*)|(.*)mongod.exe(.*)|(.*)neo4j.exe(.*)")){
                        sb.append(str).append("\n");
                    }
                }
                if (sb.toString().trim().isEmpty()) {
                    InputStream error = process.getErrorStream();
                    bufferedReader = new BufferedReader(new InputStreamReader(error));

                    while ((str = bufferedReader.readLine()) != null) {
                        sb.append(str).append("\n");
                    }
                }
                System.out.println(sb.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

我只是编辑我的解决方案,以便它可以只抓取 mysql、mongodb 和 neo4j 进程。

于 2020-07-09T12:13:52.903 回答
0

在JDK的API中,我知道没有可以查询主机操作系统进程的东西。因此,我能想到的唯一选择是使用类ProcessBuilder来执行主机操作系统命令。

由于您声明您的操作系统是 Windows,因此以下PowerShell命令将输出所选进程的进程名称和内存使用情况。

Get-Process | Where-Object {$_.ProcessName -in 'MySQL','MongoDB','Neo4j'} | Select-Object -Property 'ProcessName','WorkingSet' | Format-Table -HideTableHeaders

请注意,我不确定进程名称,因为我不使用任何MySQLMongoDBNeo4j。我假设您确实知道正确的进程名称,因此请记住在运行之前在上述命令中更正它们。

以下代码用于ProcessBuilder运行上述 [PowerShell] 命令并将命令输出重定向到System.out

import java.io.IOException;
import java.lang.ProcessBuilder;

public class PrcBldT2 {

    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
                "Get-Process | Where-Object {$_.ProcessName -in 'eclipse','oracle'} | Select-Object -Property 'ProcessName','WorkingSet' | Format-Table -HideTableHeaders");

        pb.redirectError(ProcessBuilder.Redirect.INHERIT);
        pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
        try {
            Process p = pb.start();
            int result = p.waitFor();
            System.out.println("Exit status = " + result);
        }
        catch (IOException | InterruptedException x) {
            x.printStackTrace();
        }
    }
}
于 2020-07-09T20:24:09.337 回答