This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Call Stack Memory Management


I apologize ahead of time if this isn't a proper question in the
[b]gcc-libstdc++[/b] section.

Here's my problem:

I have algorithms coded in c++ that are very recursive in nature. This is
something I'll probably not be able to change. So given this, I noticed
something annoying when I execute my code. Here's a sample program and
analysis of what I'm experiencing...

*****************************************************************************
#include <iostream>
#include <QApplication>
using namespace std;

int c = 0;
void RecursiveFunction(int n);

/*--------------------------------------------------------------------------*/
int main(int argc, char** argv)
{      
      qDebug("::1");
      RecursiveFunction(1);
      qDebug("::2");
      sleep(5);


      c = 0;
      qDebug("::3");
      RecursiveFunction(2);
      qDebug("::4");
      sleep(300);
      
      return 0;
}
/*--------------------------------------------------------------------------*/

void RecursiveFunction(int n)
{
      c++;
      if(c > 1000000*n)
      {
         return;
      }
      else
      {
         void (*fPtr)(int) = RecursiveFunction;
         fPtr(n);
      }
}

*****************************************************************************

Here's the standard error and top output...

*****************************************************************************
./mem_test 
::1
::2

... after the "::2", here's what top outputs...
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
11680 pseudosig 20   0 48200  33m 2432 S    0  1.2   0:00.04 mem_test



./mem_test 
::1
::2
::3
::4

... after the "::4", here's what top outputs...
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11680 pseudosig 20   0 79452  63m 2432 S    0  2.3   0:00.10 mem_test
*****************************************************************************

Okay, so after the recursive function gets called the first time in main, it
grows the stack (I'm assuming call stack) by 33m, and then after it fully
returns and is sleeping after "::2", the stack doesn't resize back down.
Then the same happens after the "::4" line, but by 2 fold.

My problem is that my recursive functions are consuming memory (from the
call stack?) and after the recursion fully returns, the size of the call
stack seems to stay the same, even though the function(s) has been popped
off the call stack. How can I dynamically trim the stack after the recursive
function calls have happened... or can I?

I'm sure this probably happens because the C++ runtime will allocate memory
as necessary to grow the stack. When a function returns, it moves the stack
pointer back down, but it doesn't return the frame to the OS. (If it did,
functions calls would be unbearably slow. Besides, what's the kernel going
to do with it? Give it to some other program, when it's almost certain
you're going to grow into that space again?) When you put more stuff on the
stack, it grows through the area that's already been allocated. When it hits
the ceiling again, it will allocate more from the OS.

But is there a way to return frames that aren't in the scope of the current
stack pointer? Is it a compiler option, or something I would handle in the
c++ code... or solveable at all?

Thanks...
-- 
View this message in context: http://www.nabble.com/Call-Stack-Memory-Management-tp18632819p18632819.html
Sent from the gcc - libstdc++ mailing list archive at Nabble.com.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]