接收数据会导致打开太多文件"

时间:2018-03-21 14:51:49

标签: java android networking zeromq

在我的客户端,我通过ZeroMQ接收了大量输入,需要不断更新。我的服务器是用python编写的,但这不重要。这就是我在MainActivity中所做的事情:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /********************************NETWORK********************************/
        new NetworkCall().execute("");
    }

    private class NetworkCall extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            while (true) {
                try {
                    ZMQ.Context context = ZMQ.context(1);

                    // Connect to server
                    ZMQ.Socket requester = context.socket(ZMQ.REQ);
                    String address = "tcp://xxx.xx.xx.xx";
                    int port = 5000;
                    requester.connect(address + ":" + port);

                    // Initialize poll set
                    ZMQ.Poller poller = new ZMQ.Poller(1);
                    poller.register(requester, ZMQ.Poller.POLLIN);

                    requester.send("COORDINATES");

                    //while (true) {
                    String data;
                    poller.poll();

                    data = requester.recvStr();
                    System.out.println(data);

                    if (data == null) {
                        try {
                            sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } requester.close();
                } catch (IllegalStateException ise) {
                    ise.printStackTrace();
                }
            }
        }

        @Override
        protected void onPostExecute(String result) {
        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }
}

在我的设备上执行此代码后,我会收到5-9个输入数据字符串,我从服务器收到这些字符串,但随后会出现以下异常:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                  Process: com.example.viktoria.gazefocus, PID: 31339
                  java.lang.RuntimeException: An error occurred while executing doInBackground()
                      at android.os.AsyncTask$3.done(AsyncTask.java:353)
                      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
                      at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:271)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                      at java.lang.Thread.run(Thread.java:764)
                   Caused by: com.example.viktoria.gazefocus.zmq.ZError$IOException: java.io.IOException: Too many open files
                      at com.example.viktoria.gazefocus.zmq.Signaler.makeFdPair(Signaler.java:94)
                      at com.example.viktoria.gazefocus.zmq.Signaler.<init>(Signaler.java:50)
                      at com.example.viktoria.gazefocus.zmq.Mailbox.<init>(Mailbox.java:51)
                      at com.example.viktoria.gazefocus.zmq.Ctx.<init>(Ctx.java:128)
                      at com.example.viktoria.gazefocus.zmq.ZMQ.zmq_ctx_new(ZMQ.java:244)
                      at com.example.viktoria.gazefocus.zmq.ZMQ.zmqInit(ZMQ.java:277)
                      at org.zeromq.ZMQ$Context.<init>(ZMQ.java:269)
                      at org.zeromq.ZMQ.context(ZMQ.java:254)
                      at com.example.viktoria.gazefocus.MainActivity$NetworkCall.doInBackground(MainActivity.java:73)
                      at com.example.viktoria.gazefocus.MainActivity$NetworkCall.doInBackground(MainActivity.java:67)
                      at android.os.AsyncTask$2.call(AsyncTask.java:333)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                      at java.lang.Thread.run(Thread.java:764) 
                   Caused by: java.io.IOException: Too many open files
                      at sun.nio.ch.IOUtil.makePipe(Native Method)
                      at sun.nio.ch.PipeImpl.<init>(PipeImpl.java:42)
                      at sun.nio.ch.SelectorProviderImpl.openPipe(SelectorProviderImpl.java:50)
                      at java.nio.channels.Pipe.open(Pipe.java:155)
                      at com.example.viktoria.gazefocus.zmq.Signaler.makeFdPair(Signaler.java:91)
                      at com.example.viktoria.gazefocus.zmq.Signaler.<init>(Signaler.java:50) 
                      at com.example.viktoria.gazefocus.zmq.Mailbox.<init>(Mailbox.java:51) 
                      at com.example.viktoria.gazefocus.zmq.Ctx.<init>(Ctx.java:128) 
                      at com.example.viktoria.gazefocus.zmq.ZMQ.zmq_ctx_new(ZMQ.java:244) 
                      at com.example.viktoria.gazefocus.zmq.ZMQ.zmqInit(ZMQ.java:277) 
                      at org.zeromq.ZMQ$Context.<init>(ZMQ.java:269) 
                      at org.zeromq.ZMQ.context(ZMQ.java:254) 
                      at com.example.viktoria.gazefocus.MainActivity$NetworkCall.doInBackground(MainActivity.java:73) 
                      at com.example.viktoria.gazefocus.MainActivity$NetworkCall.doInBackground(MainActivity.java:67) 
                      at android.os.AsyncTask$2.call(AsyncTask.java:333) 
                      at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                      at java.lang.Thread.run(Thread.java:764) 

显然有太多文件是打开的。经过研究(我使用的是Ubuntu 16.04),我用ulimit更改了ulimit -n 10000。这种例外仍然会发生。有时我得到更多的输入数据,有时更少。此外,如果我将Executor executor = Executors.newFixedThreadPool(5);设置为onCreate()方法,则不会发生任何变化。

如何克服这个问题?

感谢阅读!

2 个答案:

答案 0 :(得分:1)

你有泄密,因为你没有关闭/结束/释放某些东西。我认为在关闭请求者之后必须终止上下文:context.term() ...

答案 1 :(得分:0)

嗯,在设计中,信令/消息设置成本的基础设施不可忽视。一些用例更耐用,有些用例更少。

每个方法调用总是获得一个新的 Context() 实例,并通过清理调用立即将其抛出 .term() * < / strong> -method肯定比拥有一个挂起的应用程序或冷冻设备更好,但它远非公平的设计,尊重过程延迟和“生态”资源。

最好首先设置资源的半持久性基础架构(每个Context() - 实例通常是一个非常昂贵的玩具实例化(截至2018年至第一季度的API 4.2+),对于{{1}而言并不那么尖锐} -instances,但类似于Socket()和它的所有内部 - AccessPoint(s)注册挂钩,但原则也可以在'em'上扩展。

对代码的早期重新分解将有助于不使用昂贵的资源作为“可消耗的一次性”扩展案例。

部分:

Poller()

正是一种资源破坏性的反模式,总是存在重复的延迟,甚至存在远程挂断和远程拒绝以及类似问题的风险。