I'm using DDMS to monitor threads in my app, and I see that my app has a bunch of native threads as shown in follow picture. And time to time, the number of native threads increased as user interact with my app, which cause my app sometime does not serve as I expect. Is there anyway to kill these native threads?
-
It's the same with as with every thread. You don't kill them but send them a signal that they should stop. I guess you're in an endless loop inside the threads? – davidgiga1993 Jul 08 '15 at 05:15
-
How are you creating the threads? Are they from your code, or from the Android system? If they're created by Android, I don't think you can interact with them (at least not without root), as they're managed by the system as part of the application lifecycle. – Techwolf Jul 08 '15 at 05:23
-
These threads seems to be created by NsdManager, and I cannot control how it is created. It seems like there is bug in NsdManager, I guess. – congtrungvnit Jul 08 '15 at 07:08
1 Answers
There is no such thing as a "native thread" on Android, although some people might use that to refer to threads that are not attached to the VM (which would also make them invisible to DDMS). The threads happen to be executing (or waiting) in native code at the time you did a thread dump, but may spend most of their time executing bytecode. (A list of Dalvik thread states is available here.)
The names of the threads suggests that they were created without being given an explicit name. The one thread with a name, NsdManager probably exists because you're using NsdManager, which "responses to requests from an application are on listener callbacks on a seperate thread" [sic].
It's possible that you can glean some useful information from a stack trace. In DDMS, double-click the thread to get a backtrace. On a rooted device, you can kill -3 <pid>
to get a full dump, including native stack frames.
Killing arbitrary threads is not allowed, as they might be holding locks or other resources. If you can determine what is starting them, and that they are unnecessary, you can prevent them from being started in the first place.
-
These threads seems to be created by NsdManager, and I cannot control how it is created. It seems like there is bug in NsdManager, I guess. – congtrungvnit Jul 08 '15 at 07:08
-
They might be part of a thread pool created deliberately. You'd need to dig into the code to figure out what it's doing and why. What makes you think there's a bug? – fadden Jul 08 '15 at 15:20