0

I had spent quite a bit of effort ensuring that a C++ application I wrote was consuming very little memory. I then ran top (and other similar Linux commands) and was disappointed to see my application was taking more than I thought it should be. I spent some time digging into where that memory was going, but that was somewhat fruitless. I finally wrote the most simple application I could think of:

int main() {
    for(;;)
        ;
}

and ran top on that. Top reported that my minimalist app was consuming 728K RES and 4,224K VIRT! When I used clang++ instead of g++, that increased to 1,612K RES and 13,264 VIRT! What the heck? I remember the old DOS days when applications couldn't exceed 640K. Now a simple empty for loop is taking even more than that? Clearly I'm missing something. What is it?

BTW, I'm running Mint 8.1.

user545226
  • 103
  • 1
  • 8
  • 1
    You forget that your program consists more than just `main()`. It is linked with both the C and the C++ standard library. – Sam Varshavchik May 30 '17 at 02:58
  • http://www.fefe.de/dietlibc/ – Henri Menke May 30 '17 at 02:59
  • So when I new 100 objects of size 4 bytes, the size increases to 1720K RES & 13,268K VIRT. Why would allocating 400 bytes increase my memory usage by ~1M? – user545226 May 30 '17 at 03:39
  • @user545226, for *dynamic* memory allocations, the memory allocation system your implementation uses, (whatever is used by `operator new`, `malloc`, etc) usually requests more memory from the Operating System than you've requested, this prevents always making system calls whenever you need memory. And deleting the objects may not necessarily return the memory either. On the other hand, the compiler may optimize differently for different programs. Even if the difference is in the order of declaration of two objectss – WhiZTiM May 30 '17 at 03:49

1 Answers1

0

First of all, your minimal program invokes Undefined Behavior. Yes, infinite loop in C++ invokes Undefined Behavior.

More importantly, main() is typically not the first function executed in your program. There are a lot of structures that are setup, global objects may or may not be initialized before main.

Your tool-chain by default (at least popular ones), will link other libraries including (but not limited to):

  • Your tool-chain's C++ runtime library (which supports your C++ program with some essential services)
  • The C++ standard libraries, most likely the C standard library too
  • Your Operating System libraries (How else does your program communicate with the Console?)
  • and probably more.

Your executable itself will contain some additional structure to enable the Operating System load your program in memory, resolve external libraries to be loaded and more.

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
  • Gnome-calculator reports 37,640K RES and 539,504K VIRT. – user545226 May 30 '17 at 03:57
  • I didn't mean to hit enter, and it won't let me edit a comment over 5 minutes old. What I meant to say is that a simple app like gnome-calc is that big, then I wonder how big a more involved app would be. I was hoping to run my Qt app on Android, but now I'm worried that it might get too big. – user545226 May 30 '17 at 04:20
  • @user545226, sorry pal, in this business, you shouldn't always speculate. And we can't help you speculate either. I did say you should try a simple one out first and watch the behavior. – WhiZTiM May 30 '17 at 04:24
  • Does the standard C++ lib actually only get loaded once across the entire OS since it's a shared library? If so, is there a way for me to tell how much memory is dedicated to only my app? (I'm talking code, not internal static vars and stuff like that.) – user545226 May 30 '17 at 04:37
  • @user545226, it depends on your compilation flags and/or toolchain options... It's possible to link your toolchain's C++ standard library statically. It's also possible to have multiple copies of different C++ standard libraries loaded in memory by different apps... – WhiZTiM Jun 04 '17 at 21:43