6

Is there any threads limit on android?

I'm creating a lot of threads in the app and thread count is unknown in compile-time. How can i get allowed threads count?

i'm getting:

05-11 16:24:19.441  11131-11194/? E/dalvikvm﹕ Thread creation failed (err=Invalid argument)
05-11 16:24:19.461  11131-11131/? A/libc﹕ Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1), thread 11131 (id.mykeyboard)

It's not a good idea to handle exception as i have to choose another approach to deal with it and approach depends on max threads limit.

4ntoine
  • 19,816
  • 21
  • 96
  • 220
  • Why don't you use AsyncTask instead of your own threads? It uses a ThreadPool which solves any of your problems about maximum threads. – Kostas Kryptos May 11 '14 at 10:39
  • AsyncTask is pure android thing. I need to test concurrency in pure java tests first. For me it's more comfortable to use Threads. Not sure using AsyncTask can solve it – 4ntoine May 11 '14 at 10:41
  • So why don't you use a ThreadPool? – Kostas Kryptos May 11 '14 at 10:41
  • Using ThreadPool does not solve it as it means MAX_THREADS_LIMIT threads will run concurrently. I need more fine grained control - if needed threads count if less than MAX_THREADS_LIMIT i will use threads. Otherwise i will use completely different approach. – 4ntoine May 11 '14 at 10:43
  • MAX_THREADS_LIMIT is hardware, JVM and OS specific. Theoretically MAX_THREADS_LIMIT = a process's user address space divided by the thread stack size, but it is almost impossible to find the absolute number on the fly. – Kostas Kryptos May 11 '14 at 10:51
  • See post here: http://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support – Lavekush Agrawal May 11 '14 at 10:51
  • Thanks for the link, but it does not have an answer. Sorry, it has the answer "it depends". As the app is running (my code is running) i have concrete device with concrete Android OS version so the question is much more detailed than on the link. Is there any possibility to get the value on-the-fly? – 4ntoine May 11 '14 at 10:54
  • How many threads are you creating? Really you don't want more threads than logical cores (assuming you're worried about performance) – Richard Tingle May 11 '14 at 13:28
  • Threads count is unknown in compile-time and it depends on input data. Not sure you're right. If it is as you say then threads count in ALL processes running concurrently should be less than cores count which is obviously ridiculous. – 4ntoine May 11 '14 at 17:13

2 Answers2

0

There is no maximum enforced by code that I am aware of. That being said, even for network I/O, diminishing returns will set in fairly quickly, since the CPU speed of Android devices is fairly small, and RAM is fairly limited.

Otherwise use the other "approach to deal with it" that you referenced in the question.

  • I've already replied that using AsyncTask does not solve the problem. Using pool (for AsyncTask or ThreadPool) means running only N threads concurrently and it does not reduce invocation time as expected. – 4ntoine May 11 '14 at 17:11
  • Alright, thanks for letting me know. I did not see that! I'll edit out that paragraph. – explodingcreeperssss May 11 '14 at 17:31
0

In general,you can't create threads more than 1024 that the fd limited. And lots of objects, e.g. the socket,file and other which support ipc trans will use fds. One thread will cost 1MB memeroy.

The threads count may be 300-400 at most time. Less threads,Better Performance.

haoxiqiang
  • 359
  • 2
  • 4