问题
I have a problem where I need to find out the hard and soft open file limits for the process in linux from within a java/groovy program. When I execute ulimit from the terminal it gives separate values for hard and soft open file limits.
$ ulimit -n
1024
$ ulimit -Hn
4096
But, if I execute it in groovy, it ignores the soft limit and always returns hard limit value.
groovy> ['bash', '-c', 'ulimit -n'].execute().text
Result: 4096
groovy> ['bash', '-c', 'ulimit -Hn'].execute().text
Result: 4096
Please let me know if I am missing something. I have used ubuntu 12.04, Groovy Version: 1.8.4 JVM: 1.6.0_29 for this execution.
Update: I tried the same thing in Java.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
public class LinuxInteractor {
public static int executeCommand(String command, boolean waitForResponse, OutputHandler handler) {
int shellExitStatus = -1;
ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
pb.redirectErrorStream(true);
try {
Process shell = pb.start();
if (waitForResponse) {
// To capture output from the shell
InputStream shellIn = shell.getInputStream();
// Wait for the shell to finish and get the return code
shellExitStatus = shell.waitFor();
convertStreamToStr(shellIn, handler);
shellIn.close();
}
}
catch (IOException e) {
System.out
.println("Error occured while executing Linux command. Error Description: "
+ e.getMessage());
}
catch (InterruptedException e) {
System.out
.println("Error occured while executing Linux command. Error Description: "
+ e.getMessage());
}
return shellExitStatus;
}
public static String convertStreamToStr(InputStream is, OutputHandler handler) throws IOException {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is,
"UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
String output = new String(buffer, 0, n);
writer.write(buffer, 0, n);
if(handler != null)
handler.execute(output);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
public abstract static class OutputHandler {
public abstract void execute(String str);
}
public static void main(String[] args) {
OutputHandler handler = new OutputHandler() {
@Override
public void execute(String str) {
System.out.println(str);
}
};
System.out.print("ulimit -n : ");
LinuxInteractor.executeCommand("ulimit -n", true, handler);
System.out.print("ulimit -Hn : ");
LinuxInteractor.executeCommand("ulimit -Hn", true, handler);
}
}
Output for this program:
$ java LinuxInteractor
ulimit -n : 4096
ulimit -Hn : 4096
Why is the behavior same in Java. Is java setting ulimit by any chance. Thanks in advance.
回答1:
Groovy is setting this limit to maximum in it's start-up scripts.
In startGroovy:160
:
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query businessSystem maximum file
descriptor limit: $MAX_FD_LIMIT"
fi
fi
It turns out jvm is setting this limit by itself. I'm not sure why groovy is changing it too. Try:
java -XX:-MaxFDLimit LinuxInteractor
It will disable this behavior.
来源:https://stackoverflow.com/questions/10877271/finding-hard-and-soft-open-file-limits-from-within-jvm-in-linux-ulimit-n-and-u