Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I would like to design a tool to find the stack size of a running program. I understand the stack is managed by set of pointers like frame pointer, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2011
    Posts
    21

    finding the stack size of a running program


    Hi,

    I would like to design a tool to find the stack size of a running program.
    I understand the stack is managed by set of pointers like
    frame pointer, stack pointer and base pointer but which are accessible to
    only processor.

    can i access those in my program so that i can find my growing stack size.
    or is there any other way through which we can find growing stack size.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,392
    This is not simple. You can find how much space is reserved for stack in /proc/[pid]/maps, but a running program will vary over time with the amount of stack it is using. Stack is used for function access, automatic (local) variables, etc. If your program is very recursive, then it can use a lot of stack. If it uses a lot of big local variables, then that will add to the stack requirements. So, I think the question should be, why do you need to know this? Are you having stack-overflow problems?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Dec 2011
    Posts
    21
    No I am not facing any overflow issues.
    I am just trying to figure out is there any way to do it.

    even if it is hard i just want to try once.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,392
    Doing it inside your application isn't hard. It is doing it externally that I am still trying to find out for you.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Dec 2011
    Posts
    21
    Quote Originally Posted by Rubberman View Post
    Doing it inside your application isn't hard. It is doing it externally that I am still trying to find out for you.
    Thank you very much , i really appreciate your help.
    mean while can you give me a hint on how to find size from inside
    the application, i really want to try that.

    Any ways i will do some google-ing and post here if i find any.


    Thanks again.

  6. #6
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Like rubberman said, searching through the output of /proc/self/status for VmStk will give you the size of your stack segment.

    To get the size of the actual used stack at the current time though, you can use /proc/self/stat to get 'startstack' and 'kstkesp', which will give you the current stack size. See 'man 5 proc'.

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,392
    This script will work. You can us it from an application if necessary.

    Code:
    heap=$(awk '{print $23}' /proc/3045/stat) ; echo $heap
    Replace 3045 with the pid of your process. Also, you can simply open and read /proc/pid/stat as a file in your application, and look at the 23rd entry in the file.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •