0

For testing purposes, I want to know if there is a flag I can pass to java in order to limit the total execution time and memory usage of a .class file.

Motivation: let's say that I automatically download someone else's code and I run it automatically, but I don't know if the code is (unintentionally) buggy and will crash my automated system if it is caught in an infinite loop or is increasingly using memory to the limit of the system.

I read here that using the flag -Xmx you can limit the memory usage, but I can't seem to find the way to limit running time. Reading the documentation it seems like there is a way of limiting the CPU usage during execution, but that is not what I want, I want the program ended (killed if necessary) if it has been running for more than, say, 5min.

Example of what I want:

java -Xmx1m -time5m a_java_program

for limiting memory usage to 1Mb and time to 5min.

Is there something like this for java?

Community
  • 1
  • 1
Pablo Rivas
  • 941
  • 9
  • 16
  • 1
    Java alone probably can't do this, but I would imagine that your OS could. What OS are you running it on? – Makoto Sep 11 '15 at 17:01
  • 1
    In Linux you could write a script that polls "ps" and if after 5 mins your process is still there it will kill it. That should be easy to setup. – Oleg Dulin Sep 11 '15 at 17:03
  • I am in Linux and MacOS. I was afraid of this. So, my only option would be to launch it as an OS process and keep track of it that way, right? – Pablo Rivas Sep 11 '15 at 18:10

1 Answers1

2

Note that the Xmx option will limit the memory to the whole virtual machine, not a single class. For the running time, unless you have the full source code and you can run a profiler, you are in the same situation. A whole different thing would be if you were able to obtain the source code. Then you have several options to profile it for running time and memory usage (bear in mind that there will be some overhead by the profiler). For example, Eclipse: http://www.eclipse.org/tptp/home/documents/tutorials/profilingtool/profilingexample_32.html

Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • Thanks for pointing this out. So, `Xmx` will limit the memory of whatever `a_java_program` is (in the example I provided) independently if it is a single class or more? This is good to know, and I think it will still do what I need. With respect to profiling, I do not want to go that far, but I understand that there are not many options. I think for the sake of simplicity and automation, it is easier to launch it as an OS process and keep track of it. Thanks Josep, voting you up now. – Pablo Rivas Sep 11 '15 at 18:18
  • There are actually lots of options for profiling. For quick and automated testing just call the command prefixed by `time` and you will get the running time and you can monitor memory usage in another process and kill it whenever necessary as Oleg mentioned in the comment above. Take a look at this: http://www.binarytides.com/linux-command-check-memory-usage/ – Josep Valls Sep 11 '15 at 20:45