views:

111

answers:

3

When writing in C, how can I tell how much stack space is available in memory when I launch a program? How about heap space?

How can I tell how much memory is being used during the execution of my program?

+2  A: 

There is a philosophy that when you need to ask these kinds of questions, for practical and not educational or informational reasons, then you are doing something seriously wrong.

If you are asking this for error-checking or to make sure your program has enough memory, ect... then don't wrorry about it, seriously. As for your programs memory, you can use the task manager (on windows) if this is just for debugging. If you need to know this in your program, I wouldn't count on any non-hacky solution.

Alexander Rafferty
Ha, thanks for your answer. This is purely for educational reasons. I've written so many C programs and have a vague idea of the memory-space cost/usage, but was wondering how professional developers get a concrete idea about how their programs are effecting the system.
dvanaria
Another bit of philosophy is that if your program needs memory, it should simply try to allocate it and be prepared to deal with a failure. Being judicious about what you allocate is always good practice. Unless you've been handed a requirement that you can't allocate more than _x_ amount of memory, the effects it has on the rest of the system is really a systems engineering problem. Also, don't assume the figure is fixed. Your program could be running on an operating system designed to terminate lower-priority processes to free up memory when a higher-priority process bumps the ceiling.
Blrfl
+1  A: 

Abstractions for a reason Really, your program shouldn't have this as a concern. It is an OS concern, your problem should just be efficient with what it needs and let the OS do its job.

If you insist, you could look into /proc/meminfo, brk(), getrlimit() and setrlimit() (here are some docs) with the RLIMIT_STACK and RLIMIT_DATA values for approximations and rough-ishes.

#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main (int argc, char *argv[])
{
  struct rlimit limit;

  /* Get the stack limit. */
  if (getrlimit(RLIMIT_STACK, &limit) != 0) {
    printf("getrlimit() failed with errno=%d\n", errno);
    exit(1);
  }

  printf("The stack soft limit is %llu\n", limit.rlim_cur);
  printf("The stack hard limit is %llu\n", limit.rlim_max);
  exit(0);
}

Modified from here also see man getrlimit on your system

If you state what and why you want to do this, someone may have a better method or way of doing what you want.

Aiden Bell
+2  A: 

This is all Win32-specific (not really C-specific, all just OS API):

When a thread is created, it gets 1MB stack space by default, by that can be modified in whatever CreateThread API you use.

You can peek into the thread information block to find the actual stack info, but even though this is documented, this technique isn't officially supported, see http://en.wikipedia.org/wiki/Win32_Thread_Information_Block .

Also, for a 32-bit application, you can only address up to 2GB, so for an app that by design uses lots of memory, then the thing to watch out for is the total size of the process' virtual address space (committed + reserved), which includes all heap allocations. You can programmatically access the process' virtual memory with the GlobalMemoryStatusEx API, look at the ullTotalVirtual param for virtual address space. Once your process gets close to 1.8 or 1.9GB of VAS, then heap allocations and VirtualAlloc calls begin to fail. For "normal" apps, you don't have to worry about running out of VAS, but it's always good to check for fail allocs. Also, you shouldn't get a stack overflow, unless you have a bug, or a bad design.

Chris O
This was very helpful, thanks. It's giving me a starting point.
dvanaria