获取Linux中每个进程的堆和堆栈大小 [英] Get the size of heap and stack per process in Linux

查看:1077
本文介绍了获取Linux中每个进程的堆和堆栈大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Linux中每个进程的堆和堆栈的大小.有什么办法找到它吗?

I wanted to know the size of heap and stack per process in linux. Is there any way to find it?

我发现sbrk(0)将给我堆的结尾.但是,如何找到堆的起始位置以获取堆大小?

I found out that sbrk(0) will give me the end of heap. But how can I find the start of heap to get the heap size?

关于堆栈大小,是否有任何方法可以通过任何库调用或系统调用找到每个进程的堆栈起始地址和当前堆栈指针地址?

Also on stack size is there any way to find the start of stack and current stack pointer address per process through any library calls or system calls?

推荐答案

在Linux上,您可以读取/proc/[PID]/maps并找到[heap][stack]条目.

On Linux, you can read /proc/[PID]/maps and find [heap] and [stack] entries.

但是对于通常在Linux上使用的GLIBC堆实现,堆"既包含通过sbrk()获取的内存,该内存在/proc/[PID]/maps文件中显示为[heap] ,又在中显示通过mmap()-参见此问题.因此很难确定堆的大小".

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.

maps文件中标记为[stack]的区域仅是主线程的堆栈.多线程进程将具有多个堆栈,每个线程一个.并且它们将在maps文件中显示为匿名内存-也许是.应用程序可以通过使用pthread_attr_setstack()控制用于线程堆栈的内存,并将其设置为应用程序可能使用的任何内存.

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.

这篇关于获取Linux中每个进程的堆和堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆