Reduce memory allocation GCC Team

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

I could greatly benefit from this compiler function while debugging my application, since I need to handle distribution errors correctly, which is quite difficult on my PC with more than 16 GB of RAM.

Does anyone know what this option is? I expect sth like gcc --maxalloc 1024 , which would mean that my program can allocate no more than 1024 bytes of memory.

+4
source share
3 answers

I do not know the compiler options for this. However, the ulimit Linux command can be used to limit the amount of memory that a process can use.

For example, the following command limits the size of the data segment for applications running from the current shell:

 ulimit -d 1024K 
+7
source

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

It is fully C ++, and also works on any platform with any compiler!

+2
source

You doubt it. If you need the memory resources used by the gcc or g++ at compile time, you can measure them using gcc -ftime-report (which report both the time and memory of the various GCC phases). You can reduce the resources the compiler consumes to compile using the complex GCC program arguments (describe in detail in the GCC documentation) or reduce the memory limits with the built-in ulimit bash , which interacts with setrlimit (2) syscall. Like others, you can also limit the available memory for running your program using the same ulimit and syscall built-in commands.

But you probably care about the memory resources consumed by your program. I suggest you compile g++ -Wall -g and first learn how to use valgrind (and gdb ) to debug memory leaks. You can even override malloc and free .

Alternatively, you can use the Boehm conservative garbage collector . Then you would use GC_malloc instead of malloc (or use new(gc) instead of new ) and you would no longer like free or delete . But it is a conservative garbage collector (and may leave some memory leaks when you're out of luck).

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

+1
source

Source: https://habr.com/ru/post/1440657/


All Articles