I'm developing an "Online Judge System",like SGU "http://acm.sgu.ru/"
I wangt to obtain the accurate consumption memory of running ".exe"(.c/.cpp-->.exe) using Java.
Process : submit code-->hello.cpp/.c--compile-->hello.exe--run-->results
I want to know how to obtain the consumption memory of running "hello.exe"
The code:
Runtime rn = Runtime.getRuntime();
Process proc =rn.exec("hello.exe");
Thank you for helping me.
You cannot do this using pure Java.
On a UNIX / Linux machine, you would configure your operating system to enable process accounting, then read the information logged in the "acct" file. According to the acct(5) manual entry I read, this logs "average" memory usage rather than maximum memory usage.
A Windows system is bound to be different.
GetProcessMemoryInfo will tell you how much memory a process is using once you have a handle open to it. Microsoft even has an example for using the function.
To get the process ID you're looking for, you will need to enumerate through all the processes in the system. Microsoft has an example that would be useful for that too.
Edit:
The examples are all in C. This is the language the Win32 API was designed for. From Java, you'll either need to translate it to the JNI or find a Java package that does the same things.
Related
I have a few executable tools. In my Java application I need to launch each of them for a few hundred times and measure their memory consumption based on different inputs. I am using
Runtime.getRuntime().exec(externalToolCommand);
to execute external tools. But I don't know how to measure the max memory usage of the external tools.
To make more clear I will exemplify it;
Let say I have prism.exe, mrmc.exe, and plasma.exe which are three executable external tools. I have want to know when I launch one of the tools e.g. prism.exe, how much memory it consumes. I don't need to measure my Java application memory consumption. I need only to know the external memory consumption.
Thanks.
I don't know the exact code but here is what I could think of. On Windows, you can launch a batch script say p.bat from your Java application and launch a powershell script from p.bat say q.ps1 and now you have got access to a powershell script. You can run a process monitor tool(I don't know maybe perfmon...) to measure one time memory consumption of it, log it in a text file. Terminate the process from the script. Do the whole process in loop in your Java application. Finally you have got the file containing the process memory consumption n number of times.
But Beware! it is really expensive as it involves File I/O, context switch back and forth, pipelining on the top.
With powershell, the possibilities are endless. But I am no powershell expert so pardon, I can't write the exact code for you. This answer requires a lot of research on your side on various steps involved.
Try "jvisualvm", you can find it at /bin/jvisualvm.exe
Everyone knows that a heap dump can be obtained from a running JVM. Is the other way possible? Can we start a JVM using a heap dump?
I have been having this question in mind for a long time now. If this is possible it would solve a lot of time and make thinks easy for a support engineer. It helps big time in cases where if we have to recreate some the rare problems our customer face. [Just imagine that the underlying hardware and Java runtime are the same and also all the supporting files are also present in the respective location in file system].
Added note: The intention of doing this is not when OOM occurs but at any given point after JVM starts.
No you can't.
You would need things like the current position in each open file. That affects what data is returned on a simple sequential read. The restorer would need to open each file and get it to the correct position. That may not be possible for non-seekable streams.
Program specific serialization is a much more feasible path, then setup the program from there.
Also, as Heap Dumps are usually from OutOfMemory situtaions, recreating JVM from same would again throw OutOfMemoryException. If you are taking heap dump in between, then serialize your objects and restore them when you bring up jvm.
(contents copied from comments of this question, Authors almas-shaikh and patricia-shanahan)
To create a heap dump from a running JVM you can also use jhat, or jcmd (with the GC.heap_dump command), both exist in the JDK/bin folder. MAT is one way of analyzing the contents of the dump. Java Mission Control has a tool called JOverflow that analyzes heap dumps, but only to look at memory waste patterns.
I have never heard of any way to restart a JVM from sort of image, a heap dump would not be enough at all, since it only contains the Java objects, and not the compiled code and other things.
I think you are looking for tools like Java Mission Control and Chronon DVR (commercial). These help you incident analysis, event collection and profiling, time travel debugging (as phrased by chronon)
As per their documentation:
Java Mission Control
Java Flight Recorder and Java Mission Control together create a
complete tool chain to continuously collect low level and detailed
runtime information enabling after-the-fact incident analysis. Java
Flight Recorder is a profiling and event collection framework built
into the Oracle JDK. It allows Java administrators and developers to
gather detailed low level information about how the Java Virtual
Machine (JVM) and the Java application are behaving. Java Mission
Control is an advanced set of tools that enables efficient and
detailed analysis of the extensive of data collected by Java Flight
Recorder. The tool chain enables developers and administrators to
collect and analyze data from Java applications running locally or
deployed in production environments.Starting with the release of
Oracle JDK 7 Update 40 (7u40)
Some key features of Chronon Recording Server which will be useful in your case:
The Recording Server is specifically designed for long running, server
side applications that run for weeks or months at a time. The
Recording Server will take care of splitting the recording if it get
too large and flushing out old recordings.
Get rid of the need to look at long sparsely detailed log files to
debug your program. Just play back the entire execution and see
exactly what took place in your program.
The Recording Server makes it share recordings on different machines
among team members or across multiple teams.
I'd like to write a little program just to display the CPU usage as a percent, like the task manager does. I know intermediate C++ and Java. Is there a way to do this with either language? If so, perhaps a short example? I saw some page of a C++ command, but I couldn't make heads or tails of it.
I'm using Windows 7 on one computer and XP on the other. As for the multiple core response, I simply want to display the CPU usage percent as the task manager does, even with a multiple core processor.
double sysLoad = ManagementFactory.
getOperatingSystemMXBean().
getSystemLoadAverage();
does not work on every platform,
returns an average load of the past one minute
EDIT:
Recently found this one
http://sellmic.com/blog/2011/07/21/hidden-java-7-features-cpu-load-monitoring/
In Java 7 you can use com.sun.management package to get process load or system load.
usage:
OperatingSystemMXBean osBean = ManagementFactory
.getPlatformMXBean(com.sun.management.OperatingSystemMXBean.class);
// What % CPU load this current JVM is taking, from 0.0-1.0
System.out.println(osBean.getProcessCpuLoad());
// What % load the overall system is at, from 0.0-1.0
System.out.println(osBean.getSystemCpuLoad());
there are some system call functions provided in "windows.h", such as GetProcessorInfo(), CallNTPowerInformation(), GetTickCount(), QueryPerformanceFrequency(), QueryPerformanceCounter() and so on. You can google them, or find them in MSDN. I hope this answer can help you.
The answer is going to be platform specific, and differs between the Java and C++ cases. At some level you need to interface with OS specific APIs to extract the statistics. There are four possible approaches:
Call an appropriate external system monitoring command (system specific) and "scrape" the output of the command. You can do this from Java or C++.
In C++ (or using JNI / JNA in Java ... if you really have to), make calls on the OS-specific native APIs that provide the information used by the external system monitoring.
In Java, use some existing 3rd-party library that deals with the system specific JNI/JNA stuff. Possible examples include JavaSysMon and SIGAR ... though I can't make specific recommendations.
You could use the Java monitoring APIs ... but they don't provide enough information. You can get information about this processes resource usage, and the overall load average, but you can't get information about other processes.
Now the problem with all of the above (except, possibly, the last one) is that porting to a new platform requires work.
CPU usage is an operating system property, not a language property. The way to retrieve it would be specific to the OS you're using (which you fail to identify).
In addition, "CPU usage" is a very nebulous term anymore, with multiple cores, et al.
In Java you can do it using JavaSysMon
Assuming you're using Windows system, you can use Windows Management Instrumentation (WMI). It's powerful once you get it working. I never did it using Java. Of course, it's easier with C# .NET.
A good link is WMI info
If you try this, please tell me. I might be interested in helping you since this is also my interest.
IF you are using the Linux system, consider using shell scripting, like bash. The reason is shell scripting is powerful for operating system calls, like getting process ID and usage (pid command). And IT technicians are more comfortable with bash scripts than Java or C++.
If so, then how? I'm doing a team project for school. I thought that Java couldn't actually access hardware directly, since that would make it hard to be cross-platform. I need to know this, because after some quick Googling, I haven't found anything, and my team members(who want to do this and want to use Java) seem unsure of how to proceed- after apparently much more searching than I've done.
Your right in that you can't access hardware directly from Java (unless your calling on native code, but that's not what your after) since it runs in a sandboxed environment, namely the Java Virtual Machine (JVM).
However you can get some basic info from the JVM that it gathers from the underlying OS.
Take a look at using Java to get OS-level system information
What you are looking for is SIGAR API
Overview
The Sigar API provides a portable interface for gathering system information such as:
System memory, swap, cpu, load
average, uptime, logins
Per-process memory, cpu, credential
info, state, arguments, environment,
open files
File system detection and metrics
Network interface detection,
configuration info and metrics
TCP and UDP connection tables
Network route table
This information is available in most operating systems, but each OS has their own way(s) providing it.
SIGAR provides developers with one API to access this information regardless of the underlying platform.
The core API is implemented in pure C with bindings currently implemented for Java, Perl, Ruby, Python, Erlang, PHP and C#.
The amount of info you'll be able to get using native Java API's is pretty small, as your program generally only knows about the VM it's sitting on.
You can, however, call out to the command line, run native apps, and parse the results. It's not particularly good form in a Java app, however, as you lose the cross-platform benefits usually associated with the language.
You can get some basic information regarding the Processor/s using System.getEnv().
You can also use Runtime.getRuntime() - See the response of R. Kettelerij for details.
Another option is to use JMX. The MemoryMXBean for example provides some information regarding the RAM usage (heap and non-heap).
To anyone looking for a library that is still being maintained as of 2023 check out OSHI
I've written a Java file, using Jsp,servlets, that I would like to perform run-time tests on. I've never done this before and was just curious on how to go about it.
What I'm interested in knowing, besides the actual timings, is how to find cpu,memory and io utilization when running the application.Your thoughts are appreciated.
Typically you wouldn't measure these from within the application, but by running another tool on the same host.
If you just want to see the impact on the host operating system, you can use a program like top (on *nix boxes), or good old Task Manager on Windows, to see the CPU/memory/IO utilisation of your Java process (typically the servlet container such as Tomcat).
If you want more detailed information on the actual Java process itself, you can connect JConsole or jvisualvm to get VM information (including memory and CPU) for the process itself. (With Java 6 you should be able to do this from the local machine without passing any parameters to the Java process at startup; for Java 5, or remote connections, you'll need to pass command-line arguments to the Java process to allow (remote) JMX connections.)
Finally, if you want really in-depth details of the resource usage, down to the performance of various methods (which it sounds like you're after), you'll need to use a profiler. There are several of these for Java - with YourKit and JProfiler being the biggest commercial ones (in my unqualified opinion). I believe that the NetBeans IDE also has a decent profiler built-in. The process for connecting these to your application would vary depending on the app itself, but these will all typically allow you to "drill down" into the CPU time to see which classes/methods took the most cycles to execute, and likewise to drill down into memory use to see which classes are taking up the most memory.
The standard way to monitor running Java applications these days is sing JMX through the JConcole
If your a using a commercial application server like Weblogic or WebSphere these have custom and powerful management consoles that provide the monitoring information you are looking for. The technology at the heart of these consoles is still JMX so these can also be monitored and managed using the standard JConsole. This article shows how to do this for Weblogic.
I guess you need this info in the client side (browser). So it's not Java based question.
If so, here is my answer:
I prefer using FireBug and ySlow extensions. They give performance information, memory information and much more.
I combine it with using regular task-manager to view more information about the browser.
BR