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:
- Gcc throws away unused Boost symbols.
- 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
boost::shared_ptr
you might get away with less than 350K extra, so any attempt to cut down your overhead has to be specific to what you're using. – Abate