2

I am developing a programming contest manager in JAVA.

Concept of Contest Manager:

The main concept of Contest Manager is summarized below. If you have idea about it, you can skip lines before the picture.

  • Server runs on Judge PC.
  • All contestants be connected with Judge as client.
  • Contestants are provided hard copies of problem statements and they write solution on C++. They submit their solution using Contest Manger Software written in JAVA.
  • Judge have some input data in file and corresponding output data in file for every problem.
  • When a contestant submits his solution, Judge server runs it against the provided inputs by Judge.
  • Judge server then matches the output of the contestant with the correct output provided before.
  • Then Judge server gives a verdict on the basis of the matching result like Accepted, Wrong Answer, Compile Error, Time Limit Exceeded, etc.
  • Each problem has a predefined time limit. That means the submitted solution must run within a certain time period. (Usually it ranges from 1 second to 15 second)
  • The verdict of a submitted solution would be visible to all contestants. The picture below would clear the scenario. This is the submission queue and it is visible to all the contestants and judge.

enter image description here

Problem Background:

In the picture, you can see a red marked area where the time elapsed by every submitted solution is written in milliseconds. I could do easily by having the following code:

long start,end;
start = new Date().getTime();
int verdictCode = RunProgram(fileEXE, problem.inputFile, fileSTDOUT, problem.timeLimit);
end = new Date().getTime();
submission.timeElapsed = end - start;

Here, RunProgram function runs the submitted solution (program) and generates output file against an input file. If you need the details of it, ask me later, I would describe.

Main Problem:

However, There is another type of verdict called Memory Limit Exceeded which is not implemented here. I want to implement that. But getting no idea how to do it. I googled it. Somebody tell about profiling, but I am not getting how to do it properly and Do not know can it serve my purpose or not.

That means, there would be a column named Memory Elapsed like Time Elapsed. It is possible to do the thing because Online Judges like Codeforces are already showing it. But my question is, Is it possible to do the same in JAVA?

  • If yes, then how?
  • If no, then how could you be sure?

Note:

  • The software has some dependency. It must run on windows platform.
Community
  • 1
  • 1
Enamul Hassan
  • 4,744
  • 22
  • 35
  • 52

2 Answers2

1

I think you are asking about measuring statistics on native programs written in C++, correct?. You can't measure the memory usage of other programs in the OS with Java, you can only get memory information about the current JVM in Java. To measure memory usage or things like CPU usage of other processes you would need a platform-dependent solution (native code which you run with JNI). Luckily people have already implemented things like this, so that you can use plain Java objects to do what you want without having to write any C/JNI code. Check out Hyperic Sigar library for an easy way to do what you want.

mvd
  • 1,991
  • 2
  • 25
  • 41
  • Is there any brief of Hyperic Sigar Library? I think it would take time to understand. – Enamul Hassan Sep 08 '15 at 21:26
  • 2
    Just search Google. It's actually not hard to get started with this library -- http://stackoverflow.com/questions/12511956/sigar-api-for-java-need-a-guide. – mvd Sep 09 '15 at 02:28
0

I think you want the Runtime class.

Runtime runtime = Runtime.getRuntime(); 
System.out.println("Free memory: " + runtime.getFreeMemory() + " / " + rutime.getMaxMemory());
Enamul Hassan
  • 4,744
  • 22
  • 35
  • 52
Nathan Dunn
  • 417
  • 5
  • 14
  • How could I implement it? How would I get that how much memory an specific program consumed? – Enamul Hassan Sep 08 '15 at 19:58
  • You want to see if a a C++ process spawned from a java server ever exceeds memory? Partly it would depend on how RunProgram runs the C++ program and if there are any API's on the Java / C++ end . . a scriptable profiler would work. The other "semi-foolproof" way be to exec each process with a unique key and have a thread to scan / parse top and throw the error when exceeded. – Nathan Dunn Sep 08 '15 at 21:01
  • In my case, `rt = Runtime.getRuntime(); pr = rt.exec(command);` where `command` is the string that needed to run C++ program from command line. – Enamul Hassan Sep 08 '15 at 21:25
  • The right way to do it is what @mvd referred to below (Hyperic). A poor man's way would be to run a thread that queries top (if on a *nix) system and parses it. I think it would be less work to do it the right way. – Nathan Dunn Sep 08 '15 at 22:12
  • So, how could I grab/cover the idea of the right way within short time? @NathanDunn – Enamul Hassan Sep 08 '15 at 22:45
  • There is no magic bullet other than getting started. Please see @mvd's answer and helpful comment above. Here is [another link](https://support.hyperic.com/display/SIGAR/Home) to that documentation. – Nathan Dunn Sep 09 '15 at 03:04