2

I searched for an existing question/answer on this and didn't find anything.

It's easy enough to use ulimit in a shell script to determine things like the process limit and the max open files limit.

But I cannot be sure that ulimit is available. If the program (which is written in Java) is run on Windows, it will be started by a cmd script (or possibly a powershell script if we're REALLY ambitious), and ulimit is definitely not going to be available. I want to do the detection and logging in Java, not the script that starts Java.

Is the information that ulimit provides on Linux available in cross-platform native Java code?

elyograg
  • 789
  • 3
  • 14
  • Heap size is governed by JVM options in addition to ulimits; the heap size limit may be considerably smaller than the OS limit for the session. See https://stackoverflow.com/questions/992991/read-maximum-heap-space-at-runtime for some details on how to retrieve this limit. – nanofarad May 26 '18 at 21:08
  • The question in this link describes a pretty generic approach https://stackoverflow.com/questions/10877271/finding-hard-and-soft-open-file-limits-from-within-jvm-in-linux-ulimit-n-and-u – fall14123 Jul 13 '19 at 03:00

1 Answers1

2

There isn't a generic API in Java to do this. You always have to call some kind of native OS code to retrieve it. What you can do is call different scripts depending on the OS you are running in:

String osName = System.getProperty("os.name");
if ("Unix".equalsIgnoreCase(osName)) { // call a bash script }
else if ("windows".equalsIgnoreCase(osName)) { // call a PowerShell script }
else if // etc.

Some libraries, e.g. Apache Commons Lang3, provides wrappers making it easier to determine the current OS, but the gist is as above.

jingx
  • 3,698
  • 3
  • 24
  • 40
  • Thanks for the reply. I was afraid this might be the case. Since a script (either shell or cmd/powershell depending on the OS) is already being used to run the program, it makes more sense to do it there, rather than running a script that starts Java and then have Java call another script. At the moment, there's a cmd script for windows. I think it might be a good idea to learn powershell so that the startup script is more robust. – elyograg May 26 '18 at 23:24
  • FYI, this question is for work on the following issue, and/or it's parent issue: https://issues.apache.org/jira/browse/SOLR-6734 – elyograg May 26 '18 at 23:57
  • Right, if you already have a script starting the java program, it makes sense to do it there system-dependently, then pass the results in via system properties. – jingx May 27 '18 at 00:33