5

Today, during my computer sciene classes I was told that I can adjust the amount of memory my program can allocate during its compilation (using GCC, Linux). This amount is by default set to optimal mode (which means as much as possible).

I could greatly benefit from this compiler feature during debugging my application since I need to properly deal with allocation errors, which is quite tricky on my PC with over 16 GB of RAM.

Does anyone know what this option is? I expect sth like gcc --maxalloc 1024 which would mean that my program will be able to allocate at most 1024 bytes of memory.

Robin92
  • 533
  • 1
  • 6
  • 20
  • You should really pay attention to balance `malloc()`s with corresponding `free()`s instead of relying upon the program being killed in extremely low-memory conditions. –  Oct 18 '12 at 21:31
  • @H2CO3 I think that I keep track of allocated memory quite well, but I need to catch ``bad_alloc`` when allocating too much memory. – Robin92 Oct 18 '12 at 21:40
  • 2
    Sounds like [changing the stack size](http://stackoverflow.com/questions/2275550/change-stack-size-for-a-c-application-in-linux-during-compilation-with-gnu-com)? – Steve-o Oct 18 '12 at 22:35
  • Are you talking of the memory used by the `gcc` compiler during the compilation time (you could use `gcc -ftime-report` to get that), or of the memory used by the compiled executable? – Basile Starynkevitch Oct 19 '12 at 05:12
  • @BasileStarynkevitch executable :) – Robin92 Oct 19 '12 at 07:44
  • @Steve-o and how about changing heap size (new/delete)? – Robin92 Oct 19 '12 at 07:46
  • You can often change the overall data size (with `ulimit -d`). Changing only the heap size is less useful and more tricky (it requires a precise definition of what heap is). – Basile Starynkevitch Oct 19 '12 at 07:49

3 Answers3

8

I don't know of a compiler option for this. However, the ulimit Linux command can be used to limit the amount of memory a process can use.

For example, the following command would limit the data segment size of applications run from the current shell:

ulimit -d 1024K
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
2

The simplest way is to overload global new/delete operator and limit size of memory that you can allocate.

This is completely C++ and also work on any platform with any compiler!

BigBoss
  • 6,904
  • 2
  • 23
  • 38
1

You question is ambiguous. If you care about the memory resources used by the gcc or g++ compiler during compilation, you can measure them by using gcc -ftime-report (which report both time and memory of various GCC phases). You could lower the resources consumed by the compiler for compilation with tricky GCC program arguments (dive into the GCC documentation for details), or by lowering the memory limits using the ulimit builtin of the bash shell which interfaces the setrlimit(2) syscall. As others suggested, you can also limit available memory to run your program with the same ulimit builtin command and setrlimit syscall.

But you are probably caring about the memory resources consumed by your program. I suggest you to compile with g++ -Wall -g and to first learn to use valgrind (and gdb) to debug memory leaks. You could even redefine malloc and free.

Alternatively, you might consider using the Boehm's conservative garbage collector. You then would use GC_malloc instead of malloc (or use new(gc) instead of new) and you won't care any more about free or delete. But it is a conservative garbage collector (and may leave some memory leaks when you are unlucky).

To understand more the address space used by some process, use the proc(5) pseudo-file system, in particular /proc/1234/maps for the map of process 1234, or /proc/self/maps for the map of your own process. (Run cat /proc/self/maps on the terminal to see the memory map of that cat command). There is also the pmap command.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Fortunately, I don't have any memory leaks. I just need to create a huge array when the program starts (from given arguments) and properly catch an error when I don't have enough memory. – Robin92 Oct 19 '12 at 07:47
  • The use appropriately `ulimit` in your terminal before starting your program. – Basile Starynkevitch Oct 19 '12 at 07:47