• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Monitor System Processes in Java

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am attempting to implement the functionality of starting and stopping monitoring a given process by periodically displaying its CPU and Memory usage.

Through this line of code, I can obtain the list of processes that are currently running on the system, and I parse the list (which is returned as a string) and create objects stored in a HashMap.



The key of the HashMap is the process name, and the value is the object created from that process name. The information stored in that object is the process name, PID, sessionName, sessionNumber, and memoryUsage.



My question is, how can I start or stop monitoring that process? I need to be able to start or stop monitoring a given running process by periodically displaying the CPU and memory usage.

I'd like to add that this program is only a Windows program so it will not run on another OS.
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I need to be able to start or stop monitoring a given running process by periodically displaying the CPU and memory usage.


I don't understand this: how amounts displaying a process' details amount to stopping its monitoring?

But regardless: If you know the name or PID of a process you no longer want to monitor, surely there's no problem in omitting it from the output?
 
Jake Smitch
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but how do I start monitoring the process in the first place? I have the list of running processes stored in a HashMap. I want to allow the user to select a process from that list, and start monitoring it by displaying the CPU and memory usage periodically.
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think my approach would be to display a checkbox next to each process which indicates whether that particular proces is being monitored or not. The user can then set or unset any of them, submit it, and you'd adjust for which process to collect data based on that.
 
Jake Smitch
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:I think my approach would be to display a checkbox next to each process which indicates whether that particular proces is being monitored or not. The user can then set or unset any of them, submit it, and you'd adjust for which process to collect data based on that.



I get that. But my question is how do I monitor an arbitrary process? Let's say I ask the user for the process name or process id of the one which they want to monitor, for example, chrome.exe. How would I start monitoring chrome.exe? Do I need to search for the directory of the executable and somehow check the cpu and memory usage of the executable file?
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, what you're asking is how to obtain the CPU and memory usage info for a chosen process. I'm not a Windows expert, but this discussion has a number of starting points on how to do that.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:I see, what you're asking is how to obtain the CPU and memory usage info for a chosen process. I'm not a Windows expert, but this discussion has a number of starting points on how to do that.

Discussion is from 2008. Perfmon.exe is still available in Win-10 but is a GUI with no API as far as I can tell. They also mention WMI which doesn't run on Win-10.

You'll need something with an API or at least log files.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Process management is just about the polar opposite of "write once, run anywhere". Java is one of the worst language platforms I can think of to do that in, and it shows in that you have to use a rathole interface to even try.
 
Jake Smitch
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I managed to retrieve the list of processes and the memory usage by each process that I would like to monitor through this command/line of code



Now when I get the PID of the running process, from there I can get the memory usage and update it in my HashMap. However, I still struggle a way to get the CPU usage from the tasklist command. By executing this command , I am able to get the CPUTIME but not the CPU usage. Anyone have an idea of how I can get the CPU usage from tasklist?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Smitch wrote:Anyone have an idea of how I can get the CPU usage from tasklist?



I think that "Java in General" isn't the most suitable forum for this question. Let's try some other more suitable forum...
 
Jake Smitch
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Jake Smitch wrote:Anyone have an idea of how I can get the CPU usage from tasklist?



I think that "Java in General" isn't the most suitable forum for this question. Let's try some other more suitable forum...



I am writing my program in Java to complete this task. But if you find that it is not suitable in the "Java in General" forum because it involves a windows command, then by all means move my post to a more appropriate section. Thanks.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Smitch wrote:I am writing my program in Java to complete this task. But if you find that it is not suitable in the "Java in General" forum because it involves a windows command, then by all means move my post to a more appropriate section. Thanks.



It was a specific question about a specific Windows command.
 
Jake Smitch
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Jake Smitch wrote:I am writing my program in Java to complete this task. But if you find that it is not suitable in the "Java in General" forum because it involves a windows command, then by all means move my post to a more appropriate section. Thanks.



It was a specific question about a specific Windows command.



No problem, I understand. Should I create another post in a different section, or will you transfer this post?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic