Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce default C++ memory consumption?

I have a server application written in C++. After startup, it uses about 480 KB of memory on x86 Linux (Ubuntu 8.04, GCC 4.2.4). I think 480 KB is an excessive amount of memory: the server isn't even doing anything yet, no clients have been connected to the server. (See also my comment below in which I explain why I think 480 KB is a lot of memory.) The only things the server does during initialization is spawning one or two threads, setting up a few sockets, and other simple things that aren't very memory-intensive.

Note that I'm talking about real memory usage, not VM size. I measured it by starting 100 instances of my server on an idle laptop and measuring the system memory usage with 'free' before and after starting the server instances. I've already taken filesystem cache and things like that into account.

After some testing it would appear that something in the C++ runtime is causing my server to use this much memory even if the server itself doesn't do anything. For example, if I insert

getchar(); return 0;

right after

int main(int argc, char *argv[]) {

then the memory usage is still 410 KB per instance!

My application depends only on Curl and Boost. I have a fair amount of experience with C programming and I know C libraries don't tend to increase memory consumption until I use them.

Other things that I've found:

  • A simple hello world C app consumes about 50 KB of memory.
  • A simple hello world C app linked to Curl, but otherwise not using Curl, consumes about 50 KB of memory as well.
  • A simple hello world C++ app (no Boost) consumes about 100 KB of memory.
  • A simple hello world C++ app that includes some Boost headers, but does not actually use Boost, consumes about 100 KB of memory. No Boost symbols when inspecting the executable with 'nm'.

My conclusion is therefore as follows:

  1. Gcc throws away unused Boost symbols.
  2. If my app uses Boost, then something in the C++ runtime (probably the dynamic linker) causes it to use a lot of memory. But what? How do I find out what these things are, and what can I do about them?

I remember some KDE discussions several years ago about C++ dynamic linker issues. The Linux C++ dynamic linker back then caused slow startup time in KDE C++ apps and large memory consumption. As far as I know those issues have since been fixed in C++ runtimes. But could something similar be the cause of the excessive memory consumption I'm seeing?

Answers from gcc/dynamic linking experts are greatly appreciated.

For those who are curious, the server in question is Phusion Passenger's logging agent: https://github.com/FooBarWidget/passenger/blob/master/ext/common/LoggingAgent/Main.cpp

like image 922
Hongli Avatar asked Nov 14 '10 16:11

Hongli


People also ask

How can I reduce my C memory?

In C, at a much simpler level, consider the following; Use #pragma pack(1) to byte align your structures. Use unions where a structure can contain different types of data. Use bit fields rather than ints to store flags and small integers.

How does C memory work?

In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.


1 Answers

The C runtime allocates more memory than your process actually uses as part of normal operation. This is because allocating memory at the kernel level is extremely slow, and can only be done in page sized blocks (Page size is typically 4kb on x86 boxes, but it can be larger, and is usually 8kb or more on x64 machines).

Furthermore, when the C runtime receives an allocation request it cannot satisfy, it will often allocate more than is necessary, again, to remove the expense of going to the kernel most of the time.

Finally, if you're using boost goodies, they probably depend on some STL components, such as std::vector. These components allocate space for elements using std::allocator<T>, which in some instances will again allocate more space than is actually used. (In particular, node-based structures like std::map, std::set, and std::list usually do this to put the nodes of the list or tree together on the same memory page)

Long story short: Don't worry about this. Half a meg of memory isn't a lot by any stretch of the imagination (At least nowadays), and most of that is probably just amortizing the use of dynamic allocation functions. Write up your actual server, and if it's using too much memory, THEN look at ways of reducing memory usage.

EDIT: If the component of boost you're using happens to be asio, and you're using sockets, you should also know there's some memory consumed in order to maintain buffers for sockets too.

like image 117
Billy ONeal Avatar answered Sep 21 '22 02:09

Billy ONeal