How to list the number of threads ran by a process in linux
Recently I have noticed that one of our applications crossed the allowed threshold for creating new threads in a test server. After a talk with Ops guy, we gotta to know that it had around 5k+ (O_O) threads running already for no reason. Probably a memory leak that we can have it for another day.
For now, let me share you the way to find the process and its threads in linux.
Know the process id.
First of all, we need the process id in order to list out the threads being created by said process.
$ top -H -u ashokmaPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND3173 ashokma 20 0 2720980 429424 99644 S 5.6 2.7 4:49.63 Compositor3166 ashokma 20 0 2720980 429424 99644 S 4.6 2.7 1:53.81 teams
Let's take the process teams for instance. It's id is 3166
.
List the number of threads
Using the following command we can list out the number of threads.
$ ps -T -p 3166PID SPID TTY TIME CMD3166 3166 ? 00:02:02 teams3166 3168 ? 00:00:00 ThreadPoolServi3166 3170 ? 00:01:47 Chrome_ChildIOT3166 3171 ? 00:00:00 GpuMemoryThread3166 3173 ? 00:05:17 Compositor3166 3174 ? 00:00:00 ThreadPoolSingl3166 3175 ? 00:00:01 CompositorTileW3166 3176 ? 00:00:01 CompositorTileW3166 3177 ? 00:00:01 CompositorTileW3166 3178 ? 00:00:01 CompositorTileW3166 3179 ? 00:00:00 CompositorTileW3166 3180 ? 00:00:00 teams3166 3184 ? 00:00:00 Watchdog3166 3199 ? 00:00:01 SharedWorker th3166 3205 ? 00:00:00 DedicatedWorker3166 3328 ? 00:00:00 Media3166 71782 ? 00:00:00 ThreadPoolForeg3166 71915 ? 00:00:00 ThreadPoolForeg3166 71916 ? 00:00:00 ThreadPoolForeg3166 71917 ? 00:00:00 ThreadPoolForeg3166 71918 ? 00:00:00 ThreadPoolForeg3166 71919 ? 00:00:00 ThreadPoolForeg3166 71920 ? 00:00:00 ThreadPoolForeg
Pro Tip: To quickly know the number of threads just chain the above command with wordcount (
wc
) as follows:
$ ps -T -p 3166 | wc -l21# This varies realtime as the threads are being closed and created over the time.