I would like to consult memory used at the instant by MySQL, MongoDB and Neo4j using java. I've searched for it, but didn't found anything relevant. My OS is Windows and i am using Eclipse. Thanks
-
which plateform you are targeting ? – A.RAZIK Jul 09 '20 at 09:54
-
Windows, using eclipse – Rodrigo Jul 09 '20 at 09:56
-
https://stackoverflow.com/questions/47177/how-do-i-monitor-the-computers-cpu-memory-and-disk-usage-in-java I think this question at least partially covers the answer, @RodrigoCordeiro. Please, comment if more clarification would be required. – Mikhail Krutov Jul 09 '20 at 10:39
-
What data do you want to obtain? The name of the task and how much memory it is using? The amount of memory used by a task is dynamic, i.e. it is not constant. Do you want the amount of memory at the instant you ask for it, or do you want to monitor its changes over a period of time? – Abra Jul 09 '20 at 13:21
-
I want the memory at the instant used by MySQL, MongoDB and Neo4j. I only need those 3 programs – Rodrigo Jul 09 '20 at 14:52
-
@RodrigoCordeiro, did you try to read the answer ? and please don't change you question when people are answering you, you can just create new question ! – A.RAZIK Jul 09 '20 at 16:03
2 Answers
I just came up with a java code template to run a windows command, you need to run tasklist
command to have all the process table with all information including memory used by each process.
Here is the code:
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();
}
}
}
}
I just edit my solution so that it can grab just mysql, mongodb and neo4j process.

- 434
- 3
- 8
-
An excerpt from _javadoc_ of method [`exec`](https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String:A-java.lang.String:A-java.io.File-) in class `java.lang.Runtime`: _`ProcessBuilder.start()` is now the preferred way to start a process_ – Abra Jul 10 '20 at 11:55
There is nothing that I know of in the API of the JDK that can query processes of the host operating system. Hence the only option that I can think of is to use class ProcessBuilder
to execute a host OS command.
Since you state that your OS is Windows, the following PowerShell command will output the process name and memory usage of your selected processes.
Get-Process | Where-Object {$_.ProcessName -in 'MySQL','MongoDB','Neo4j'} | Select-Object -Property 'ProcessName','WorkingSet' | Format-Table -HideTableHeaders
Note that I am not sure about the process names because I don't use any of MySQL, MongoDB or Neo4j. I assume that you do know the correct process names so remember to correct them in the above command before you run it.
The below code uses ProcessBuilder
to run the above [PowerShell] command and redirects the command output to 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();
}
}
}

- 19,142
- 7
- 29
- 41