Techniques to reduce memory footprint

Avoid using recursive calls

Recursive calls grow the stack memory of the process and when it is deeply nested, it may lead to stack overflow.

Prefer lazy loading than initial loading.

For example, if we need to count the number of words in a file, it is better to read the data line rather than reading the whole file into memory and then count the words.Readingline by line into the same buffer and processing each line can do this.

Prefer Shared libraries than static libraries

When shared library is used, the physical pages associated with the shared libraries are shared for all the process using that library. But in case of static libraries, each executable has its own copy leading to huge memory utilization for all the process using that library.

Modularizing and on-demand loading

Instead of linking your executable with your big-shared library, which contains all the functionality, we can modularize and create multiple shared libraries, which shall be loaded on demand based on the configuration. One issue with this, the performance will degrade as the program will do dlsym() to locate the symbol in the library. This we shall overcome by populating the function pointer list only once by providing the initialization function which does dlsym for all the necessary symbol and store it in lookup table or array of function pointers.

Use bit mask to store flags rather than bool or char data type

When multiple options need to be specified, it is usually defined as set of integer or character values that takes lot of space

struct Options{

bool isFlag1;

bool isFlag2;

bool isFlag3;

bool isFlag4;

bool isFlag5;


Instead, it can be stored in bit mask as follows

struct Options {

char opt;


#define FLAG1   1

#define FLAG2   2

#define FLAG1   4

#define FLAG1   8

#define FLAG1   16

Using the macro values defined above you can set the options by OR’ing values to it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s