Alexis
Alexis

Reputation: 16829

How can I know to which element a thread of my android app is related?

First of all, I don't create threads manually.

I can monitor the threads of my android app :

How can I know to which elements these threads are related ?

For example all the httpXX threads or AsyncTask #X , is there a way to know where they're created in the code? Because they stay there everytime and I'm afraid to leak the memory.

In opposition I have an ImageCache that load images in threads. These thread appear in the list when I'm in the view that display the images and disappear when I close the app.

httpX , AsyncTask #X , WebViewWorkerThread , etc DON'T disappear when I close the app. So I'd like to know where I can free the memory, close connections, etc

Upvotes: 0

Views: 88

Answers (1)

Andrei Mankevich
Andrei Mankevich

Reputation: 2273

This threads are created by Android classes which internally rely on thread pools. The main idea of thread pool is not to create a new thread every time when you need to do something but reuse already started threads and keep them running even if there is no work for them now.

For example AsyncTask #X threads are created by AsyncTask internal ThreadPoolExecutor. If you look into it's source code you will notice this ThreadFactory:

private static final ThreadFactory sThreadFactory = new ThreadFactory() {
    private final AtomicInteger mCount = new AtomicInteger(1);

    public Thread newThread(Runnable r) {
        return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
    }
};

WebViewCoreThread, WebViewWorkerThread, and http0 - http11 are internal worker threads of WebView which executes all operations concerning loading html data over the network, processing, etc. on behalf of WebView.

So all this threads are managed by Android system and you don't need to do anything with them. And it's all right that they don't disappear when you close your app.

Upvotes: 1

Related Questions