socket.io-client-java icon indicating copy to clipboard operation
socket.io-client-java copied to clipboard

OutOfMemoryError: pthread_create

Open alexvimodev opened this issue 10 years ago • 15 comments

Hi,

I am using socket io library for my Android app. 

I have added the dependency as "compile 'io.socket:socket.io-client:0.7.0' ". I created the last build on 25-05-2016. Could you please fix this memory issue.

Android 4.4.2 Model : GT-I9301I

Crash report: Crashed Thread 0 java.lang.OutOfMemoryError: pthread_create (stack size 16384 bytes) failed: Try again 1 at java.lang.VMThread.create(Native Method) 2 at java.lang.Thread.start(Thread.java:1029) 3 at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:920) 4 at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1327) 5 at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:583)! 6 at io.socket.thread.EventThread.nextTick(SourceFile:71) 7 at io.socket.thread.EventThread.exec(SourceFile:52) 8 at io.socket.engineio.client.transports.PollingXHR$6.call(SourceFile:122) 9 at io.socket.emitter.Emitter.emit(SourceFile:117) 10 at io.socket.engineio.client.transports.PollingXHR$Request.onError(SourceFile:251) 11 at io.socket.engineio.client.transports.PollingXHR$Request.access$700(SourceFile:135) 12 at io.socket.engineio.client.transports.PollingXHR$Request$1.run(SourceFile:218) 13 at java.lang.Thread.run(Thread.java:841)

alexvimodev avatar Jun 17 '16 04:06 alexvimodev

+1 . It is happening to lot of my users as well.

shobhitpuri avatar Jul 07 '16 23:07 shobhitpuri

Any updates on this? @alexvimodev Were you able to figure out a solution? Thanks.

shobhitpuri avatar Aug 02 '16 17:08 shobhitpuri

Hi Shobhitpuri, I didn't get any solution for this issue. Also, I am unable to reproduce this issue in my available phones :(. I will try to simulate this issue and update you if I get any solution.

Thank you.

alexvimodev avatar Aug 05 '16 04:08 alexvimodev

I have the same issue on various Android phones (Galaxy Nexus 6, Samsung S4) OS version 5.1, 6:

Fatal Exception: java.lang.OutOfMemoryError Could not allocate JNI Env
java.lang.Thread.nativeCreate (Thread.java)
java.util.concurrent.Executors$DelegatedExecutorService.execute (Executors.java:584)
io.socket.thread.EventThread.nextTick (EventThread.java:71)
io.socket.thread.EventThread.exec (EventThread.java:52)
io.socket.engineio.client.transports.PollingXHR$1.call (PollingXHR.java:53)
io.socket.emitter.Emitter.emit (Emitter.java:117)
io.socket.engineio.client.transports.PollingXHR$Request.onResponseHeaders (PollingXHR.java:260)
io.socket.engineio.client.transports.PollingXHR$Request.access$500 (PollingXHR.java:135)
io.socket.engineio.client.transports.PollingXHR$Request$1.run (PollingXHR.java:212)
java.lang.Thread.run (Thread.java:818)

KrawczykPawel avatar Aug 05 '16 10:08 KrawczykPawel

I have similiar issue in my app

Fatal Exception: java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again at java.lang.Thread.nativeCreate(Thread.java) at java.lang.Thread.start(Thread.java:1063) at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:921) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1328) at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:584) at io.socket.thread.EventThread.nextTick(EventThread.java:76) at io.socket.thread.EventThread.exec(EventThread.java:57) at io.socket.engineio.client.transports.WebSocket$2.onFailure(WebSocket.java:147) at okhttp3.ws.WebSocketCall$1.onFailure(WebSocketCall.java:104) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)

What can be a clue is that when app crashed there was more than 2000 threads and almost all of them looked like this

OkHttp ConnectionPool at java.lang.Object.wait(Object.java) at okhttp3.ConnectionPool$1.run(ConnectionPool.java:66) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)

mgwizdala avatar Nov 07 '16 12:11 mgwizdala

Hi @alexvimodev, @mgwizdala Is there any update on this issue ? because i am getting the same problem in my app

Fatal Exception: java.lang.OutOfMemoryError: Could not allocate JNI Env at java.lang.Thread.nativeCreate(Thread.java) at java.lang.Thread.start(Thread.java:1063) at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:921) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1328) at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:584) at io.socket.thread.EventThread.nextTick(EventThread.java:71) at io.socket.thread.EventThread.exec(EventThread.java:52) at io.socket.engineio.client.transports.WebSocket$1.onMessage(WebSocket.java:104) at okhttp3.internal.ws.RealWebSocket$1.onMessage(RealWebSocket.java:62) at okhttp3.internal.ws.WebSocketReader.readMessageFrame(WebSocketReader.java:242) at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:108) at okhttp3.internal.ws.RealWebSocket.readMessage(RealWebSocket.java:97) at okhttp3.ws.WebSocketCall.createWebSocket(WebSocketCall.java:152) at okhttp3.ws.WebSocketCall.access$000(WebSocketCall.java:41) at okhttp3.ws.WebSocketCall$1.onResponse(WebSocketCall.java:97) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)

unable to figure out the reason and any solution.

Mukesh-Singh avatar Dec 14 '16 09:12 Mukesh-Singh

@mgwizdala @alexvimodev @KrawczykPawel Check https://github.com/square/okhttp/issues/2846 . I see similar behaviour in stack traces, where I see more than 1000 to 2000 threads. I am trying to distinguish if the issue is with the library or my app. I am using Retrofit and OkHttpClient.Builder for performing Rest request. Maybe my Singleton implementation is not correct because of which so many threads are being created. Are you using Retrofit too in your project by any chance?

shobhitpuri avatar Jan 23 '17 20:01 shobhitpuri

Unfortunately project that i am working on is configured to use Volley for normal http requests. Okhttp is used only by SocketIO so this bug must be related with one of this two libraries.

2017-01-23 21:31 GMT+01:00 Shobhit Puri [email protected]:

@mgwizdala https://github.com/mgwizdala @alexvimodev https://github.com/alexvimodev @KrawczykPawel https://github.com/KrawczykPawel Check square/okhttp#2846 https://github.com/square/okhttp/issues/2846 . I see similar behaviour in stack traces, where I see more than 1000 to 2000 threads. I am trying to distinguish if the issue is with the library or my app. I am using Retrofit and OkHttpClient.Builder for performing Rest request. Maybe my Singleton implementation is not correct because of which so many threads are being created. Are you using Retrofit too in your project by any chance?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/socketio/socket.io-client-java/issues/315#issuecomment-274607811, or mute the thread https://github.com/notifications/unsubscribe-auth/ABXgJKYa2B6ViJbf1wSacxdgP-xTqAAKks5rVQ43gaJpZM4I4Bwr .

mgwizdala avatar Jan 23 '17 21:01 mgwizdala

@shobhitpuri @mgwizdala, I am using only socketio library in my project. Thats why I created the issue in github with this project. But, I am not getting this crash in "socket.io-client-0.8.3" (till now).

alexvimodev avatar Jan 24 '17 04:01 alexvimodev

@mgwizdala You might be right. It seems to be the engie.io project. @alexvimodev Not sure how much of that is fixed. It might be just a matter of time. If you read https://square.github.io/okhttp/3.x/okhttp/okhttp3/OkHttpClient.html and https://github.com/square/okhttp/issues/2846, it says

OkHttp performs best when you create a single OkHttpClient instance and reuse it for all of your HTTP calls. This is because each client holds its own connection pool and thread pools. Reusing connections and threads reduces latency and saves memory. Conversely, creating a client for each request wastes resources on idle pools.

Use new OkHttpClient() to create a shared instance with the default settings:

// The singleton HTTP client.
 public final OkHttpClient client = new OkHttpClient();

Or use new OkHttpClient.Builder() to create a shared instance with custom settings:

   // The singleton HTTP client.
   public final OkHttpClient client = new OkHttpClient.Builder()
       .addInterceptor(new HttpLoggingInterceptor())
       .cache(new Cache(cacheDir, cacheSize))
       .build();

However if you see line 42 the Websocket.java file in the engine.io project (which is used by socket.io-client-java), we are creating a new instance of OkHttpClient each time doOpen() is called. So if in your app you close and open connection multiple times, then each time new instance of the client will be created and will hold its own set of connection pools. This might be what is leading to the OOM exceptions.

shobhitpuri avatar Jan 24 '17 14:01 shobhitpuri

@alexvimodev Thanks for the update. I hope the crash doesn't happen anymore with 0.8.3. I will try updating. However I've opened an issue with https://github.com/socketio/engine.io-client-java/issues/81. Please do update here in case you see crashes happening on socket.io-client-0.8.3 as well. Thanks so much!

shobhitpuri avatar Jan 30 '17 16:01 shobhitpuri

It keeps crashing a lot (v0.8.3) :( even using the created socket do send a presence flag... it is possible that only one socket could exists? In my app I've launched 2 different sockets for different functionalities

iflorit avatar Mar 20 '17 10:03 iflorit

@iflorit I am using more than one socket connection in my app. It is working fine with v.0.8.3 without this memory issue. There could be different issue in your case.

alexvimodev avatar Mar 20 '17 10:03 alexvimodev

Hi All,

I'm using this library as well in my app using v0.8.3, and I'm experiencing this OOM issue but on my investigation, the problem was being encountered when my app keep creating new socket instance due to wrong singleton socket constructor.

JayCM avatar Apr 28 '17 04:04 JayCM

For future readers:

This should be fixed in latest versions.

Related: https://github.com/socketio/engine.io-client-java/issues/81

Please reopen if needed.

darrachequesne avatar Jun 29 '22 06:06 darrachequesne