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.