[英]Android - java.net.ConnectException: failed to connect - connect failed: EMFILE (Too many open files)
I have an Android app where I am basically running an Async task to ping my server for an Access Token every 3 minutes. 我有一个Android应用程序,我基本上在其中运行一个异步任务,每隔3分钟对服务器进行一次ping通访问令牌。 But after about an hour the app freezes and I see this exception in logcat. 但是大约一个小时后,该应用程序冻结,并且我在logcat中看到此异常。 The app eventually crashes. 该应用最终崩溃。
Here is the log of the crash 这是崩溃的日志
10-25 16:38:47.527 19845-19845/com.test.app E/SharedPreferencesImpl﹕ Couldn't create directory for SharedPreferences file /data/data/com.test.app/shared_prefs/MY_SHARED_PREF.xml
10-25 16:38:47.536 19845-19845/com.test.app E/SharedPreferencesImpl﹕ Couldn't create directory for SharedPreferences file /data/data/com.test.app/shared_prefs/MY_SHARED_PREF.xml
10-25 16:38:47.545 19845-23000/com.test.app W/SpotifySDK﹕ Player::deliverAudio called with 0 frames
10-25 16:38:47.545 19845-23000/com.test.app I/SpotifySDK﹕ Got notification: Pause
10-25 16:38:47.547 19845-23000/com.test.app I/SpotifySDK﹕ Got notification: Track ended
10-25 16:38:47.552 19845-23000/com.test.app I/SpotifySDK﹕ Got notification: Track changed
10-25 16:38:47.553 19845-19845/com.test.app E/SharedPreferencesImpl﹕ Couldn't create directory for SharedPreferences file /data/data/com.test.app/shared_prefs/MY_SHARED_PREF.xml
10-25 16:38:47.560 19845-19913/com.test.app W/System.err﹕ java.net.ConnectException: failed to connect to /51.21.21.111 (port 8080) after 10000ms: connect failed: EMFILE (Too many open files)
10-25 16:38:47.572 19845-19913/com.test.app W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:124)
10-25 16:38:47.572 19845-19913/com.test.app W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
10-25 16:38:47.572 19845-19913/com.test.app W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
10-25 16:38:47.572 19845-19913/com.test.app W/System.err﹕ at java.net.Socket.connect(Socket.java:882)
10-25 16:38:47.573 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.internal.Platform$Android.connectSocket(Platform.java:190)
10-25 16:38:47.573 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.Connection.connectSocket(Connection.java:196)
10-25 16:38:47.573 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.Connection.connect(Connection.java:172)
10-25 16:38:47.573 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:367)
10-25 16:38:47.573 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
10-25 16:38:47.573 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
10-25 16:38:47.573 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
10-25 16:38:47.574 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.Call.getResponse(Call.java:267)
10-25 16:38:47.574 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
10-25 16:38:47.574 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
10-25 16:38:47.574 19845-19913/com.test.app W/System.err﹕ at com.squareup.okhttp.Call.execute(Call.java:79)
10-25 16:38:47.575 19845-19913/com.test.app W/System.err﹕ at com.test.app.async.AccessTokenUpdateTask.doInBackground(AccessTokenUpdateTask.java:57)
10-25 16:38:47.575 19845-19913/com.test.app W/System.err﹕ at com.test.app.async.AccessTokenUpdateTask.doInBackground(AccessTokenUpdateTask.java:21)
10-25 16:38:47.575 19845-19913/com.test.app W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
10-25 16:38:47.575 19845-19913/com.test.app W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-25 16:38:47.575 19845-19913/com.test.app W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-25 16:38:47.575 19845-19913/com.test.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-25 16:38:47.576 19845-19913/com.test.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-25 16:38:47.576 19845-19913/com.test.app W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
10-25 16:38:47.576 19845-19913/com.test.app W/System.err﹕ Caused by: android.system.ErrnoException: connect failed: EMFILE (Too many open files)
10-25 16:38:47.576 19845-19913/com.test.app W/System.err﹕ at libcore.io.Posix.connect(Native Method)
10-25 16:38:47.577 19845-19913/com.test.app W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
10-25 16:38:47.577 19845-19913/com.test.app W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:154)
10-25 16:38:47.577 19845-19913/com.test.app W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
10-25 16:38:47.577 19845-19913/com.test.app W/System.err﹕ ... 22 more
10-25 16:38:47.578 19845-19845/com.test.app W/art﹕ Large object allocation failed: ashmem_create_region failed for 'large object space allocation': Too many open files
10-25 16:38:47.603 19845-19845/com.test.app I/art﹕ Alloc sticky concurrent mark sweep GC freed 75611(1974KB) AllocSpace objects, 15(384KB) LOS objects, 13% free, 48MB/55MB, paused 1.317ms total 23.837ms
10-25 16:38:47.605 19845-19845/com.test.app W/art﹕ Large object allocation failed: ashmem_create_region failed for 'large object space allocation': Too many open files
10-25 16:38:47.626 19845-19845/com.test.app I/art﹕ Alloc partial concurrent mark sweep GC freed 9352(329KB) AllocSpace objects, 6(1169KB) LOS objects, 25% free, 46MB/62MB, paused 965us total 20.962ms
10-25 16:38:47.628 19845-19845/com.test.app W/art﹕ Large object allocation failed: ashmem_create_region failed for 'large object space allocation': Too many open files
10-25 16:38:47.650 19845-19845/com.test.app I/art﹕ Alloc concurrent mark sweep GC freed 399(28KB) AllocSpace objects, 0(0B) LOS objects, 25% free, 46MB/62MB, paused 922us total 21.750ms
10-25 16:45:19.014 19845-19845/com.test.app E/Surface﹕ dequeueBuffer failed (Unknown error 2147483646)
10-25 16:45:19.022 19845-19845/com.test.app E/ViewRootImpl﹕ Could not lock surface
java.lang.IllegalArgumentException
at android.view.Surface.nativeLockCanvas(Native Method)
....
10-25 22:34:42.026 22778-22778/com.test.app E/Parcel﹕ dup() failed in Parcel::read, i is 0, fds[i] is -1, fd_count is 1, error: Too many open files
Here is my analysis of this log: I understand why the app UI froze, it was due to the "dequeueBuffer failed" and the "lock surface" exception. 这是我对该日志的分析:我了解为什么应用程序UI冻结是因为“ dequeueBuffer失败”和“锁定表面”异常。 But the line before it tells me that the app is keeping too many files open from each 3 min ping I make to my server. 但是前一行告诉我,从每次对服务器进行3分钟的ping操作后,该应用程序都会打开太多文件。
I am defining my client object in the activity like below and am passing in that client object to that Async task. 我在如下所示的活动中定义我的客户端对象,并将该客户端对象传递给该Async任务。 I wrote another version of the task where I am defining the OkHttpClient object in the Async task itself but I am getting the same error. 我编写了该任务的另一个版本,其中在Async任务本身中定义了OkHttpClient对象,但遇到了相同的错误。 Here is the code in my Async task. 这是我的异步任务中的代码。
Inside the Activity 活动内部
OkHttpClient client = new OkHttpClient();
Passing the client object to the AsyncTask via the constructor 通过构造函数将客户端对象传递给AsyncTask
RequestBody body = RequestBody.create("application/json; charset=utf-8", jsonString);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
if(response.isSuccessful()) {
String responseBody = response.body().string();
return responseBody;
}
} catch (Exception e) {
e.printStackTrace();
}
Any idea how I can fix this error? 知道如何解决此错误吗?
You're creating a new OkHttpClient every time an async task executes. 每次执行异步任务时,您都在创建一个新的OkHttpClient。 The square documentation indicates that most apps use one okhttpclient instance. 方形文档指示大多数应用程序使用一个okhttpclient实例。 Try moving your okhttpclient instantiation out of the async task so that only one instance is created per application lifecycle. 尝试将okhttpclient实例移出异步任务,以便每个应用程序生命周期仅创建一个实例。
So I got the answer to this question and it has nothing to do with OkHTTP. 所以我得到了这个问题的答案,它与OkHTTP无关。 I was happening due to an another SDK which I was using. 我由于使用了另一个SDK而发生了事情。 Well I wasn't using that correctly. 好吧,我没有正确地使用它。 It was the Spotify Android SDK and basically when I am creating the player object I need to create a config for it like so 那是Spotify Android SDK,基本上,当我创建播放器对象时,我需要为其创建配置,如下所示
Config playerConfig = new Config(this,
accessToken,
getString(R.string.client_id));
All I had to do was add the following line 我所要做的就是添加以下行
playerConfig.useCache(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.