c - How can I optimize GCC compilation for memory usage? -


i developing library should use little memory possible (i not concerned anything else, binary size, or speed optimizations).

are there gcc flags (or other gcc-related options) can use? should avoid level of -o* optimization?

you library -or code in idiomatic c- has several kinds of memory usage :

  • binary code size, , indeed -os should optimize that
  • heap memory, using c dynamic allocation, malloc; should know how, , how much, heap memory allocated (and later free-d). actual memory consumption depend upon particular malloc implementation (e.g. many implementations, when calling malloc(25) in fact consume 32 bytes), not on compiler. btw, might design library use memory pools or implement own allocator (above os syscalls mmap, or above malloc etc...)
  • local variables, call frames on call stack. depend upon code (but optimizing compiler, e.g. -os or -o2 gcc, use more registers , perhaps less stack when optimizing). pass -fstack-usage gcc ask give size of every call frame , might give -wstack-usage=len warned when call frame exceeds len bytes.
  • global or static variables. should know how memory need (and might use nm or other binutils program query them). btw, declaring carefully variables inside function static lower stack consumption (but cannot every variable or every function).

notice in some limited cases, gcc doing tail calls, , stack usage lowered (since stack frame of caller reused in callee). (see this old question).

you might ask compiler pack particular struct-s (beware, slowdown performance significantly). you'll want use type attributes __attribute__((packed)), etc... , perhaps variable attributes etc...

perhaps should read more garbage collection, since gc techniques, concepts, , terminology might relevant. see this answer.

if on linux, valgrind tool should useful too... (and during debugging phase -fsanitize=address option of recent gcc).

you might perhaps use code generation options -fstack-reuse= or -fshort-enums or -fpack-struct or -fstack-limit-symbol= or -fsplit-stack ; be careful: such options make binary code incompatible existing c (and others!) libraries (then might need recompile used libraries, including libc, same code generation flags).

you should enable link-time optimizations compiling and linking -flto (in addition of other optimization flags -os).

you should use recent version of gcc. notice gcc 5.1 has been released few days ago (in april 2015).

if library large enough worth effort, might consider customizing gcc compiler melt (to find out how spend less memory). might take weeks or months of work.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -