On Linux, you can read /proc/[PID]/maps
and find [heap]
and [stack]
entries.
But for the GLIBC heap implementations usually used on Linux, the "heap" consists of both memory obtained via sbrk()
that shows up in the /proc/[PID]/maps
file as [heap]
and memory obtained via mmap()
- see this quesiton. So the "size" of the heap is going to be very hard to determine with certainty.
And the region labelled [stack]
in the maps
file is the stack for the main thread only. Multithreaded processes will have multiple stacks, one for each thread. And they will show up in the maps
file as anonymous memory - maybe. The application can control the memory used for a thread's stack via the use of pthread_attr_setstack()
and set it to any memory the application might use.