views:

64

answers:

2

Hello,my friend.

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.

+1  A: 

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.

Stephen C
oh,I'm on a Windows Machine..._baseMemory=MemoryMXBean.getHeapMemoryUsage().getUsed()_memoryUsed = (int) ((_memoryBean.getHeapMemoryUsage().getUsed() - _baseMemory) / 1000); Is this ok?
huaigu
@huaigu: MemoryMXBean only deals with the memory allocated to the JVM, not to external processes.
Catchwa
Who can help me?
huaigu
@huaigu - if you spent time answering other peoples' questions, you would have enough reputation points to offer a "bounty" on your questions. That's the way SO is supposed to work.
Stephen C
Oh,I'm sorry!I'm a freshman in using SO,and I will be more careful next time. Thank you for reminding me.
huaigu
Another alternative would be to change the tags to something that is more likely to attract the attention of people who might be able to answer. This question is really about how to find out about memory usage of a process on Windows ... not Java or online judges.
Stephen C
A: 

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.

Chris Smith
Thank you very much.I will have a try.
huaigu