Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在linux(ulimit-n和ulimit-Hn)中从jvm中查找硬打开和软打开文件限制_Java_Linux_Groovy_Jvm_Ulimit - Fatal编程技术网

Java 在linux(ulimit-n和ulimit-Hn)中从jvm中查找硬打开和软打开文件限制

Java 在linux(ulimit-n和ulimit-Hn)中从jvm中查找硬打开和软打开文件限制,java,linux,groovy,jvm,ulimit,Java,Linux,Groovy,Jvm,Ulimit,我有一个问题,需要从java/groovy程序中找出linux中进程的硬文件和软文件打开限制。当我从终端执行ulimit时,它会为硬打开文件限制和软打开文件限制提供单独的值 $ ulimit -n 1024 $ ulimit -Hn 4096 但是,如果我在groovy中执行它,它会忽略软限制并总是返回硬限制值 groovy> ['bash', '-c', 'ulimit -n'].execute().text Result: 4096 groovy> ['bash', '-

我有一个问题,需要从java/groovy程序中找出linux中进程的硬文件和软文件打开限制。当我从终端执行ulimit时,它会为硬打开文件限制和软打开文件限制提供单独的值

$ ulimit -n
1024
$ ulimit -Hn
4096
但是,如果我在groovy中执行它,它会忽略软限制并总是返回硬限制值

groovy> ['bash', '-c', 'ulimit -n'].execute().text  
Result: 4096

groovy> ['bash', '-c', 'ulimit -Hn'].execute().text 
Result: 4096
如果我遗漏了什么,请告诉我。我使用了Ubuntu12.04,Groovy版本:1.8.4JVM:1.6.0_29来执行此操作

更新: 我在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);
    }

}
此程序的输出:

$ java LinuxInteractor 
ulimit -n : 4096

ulimit -Hn : 4096
为什么Java中的行为相同。java正在设置ulimit。
提前谢谢

Groovy在其启动脚本中将此限制设置为最大值

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

事实证明,jvm是自己设置这个限制的。我不知道为什么groovy也在改变它。尝试:

java -XX:-MaxFDLimit LinuxInteractor

它将禁用此行为。

谢谢您的回复@Banthar。我已经用java执行更新了我的问题,在java中我发现了类似的行为。