Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I further reduce "private bytes" memory usage of this tiny MSVC++ project?

I'm asking this out of curiosity rather than real need, but can the memory usage of this tiny MSVC++ program be reduced further? Source file on BitBucket.

The program was compiled with "optimize for code size". It creates a message-only window and sets a keyboard hook, showing a tray icon in response to Caps/Num/Scroll Lock key presses.

According to VMMap, the private bytes are allocated as follows:

260 KB: Image
252 KB: Heap
240 KB: Page Table
 24 KB: Stack
 24 KB: Private Data
------
800 KB  TOTAL

Image

The application itself uses only 20 KB of this; the rest is consumed by a dozen of DLLs. Looks like this is as small as it gets.

Heap

The program only allocates about 3 KB of data on the heap: exactly three instances of a certain class. The rest must come from the CRT and/or the standard OS code.

Can this be reduced perhaps? This looks like a prime candidate for savings.

Page Table

The total virtual size of this program is 44 MB, which is about 11k pages. That's 22 bytes per page on average (though presumably a bunch of entries are set aside unused). So this probably can't be reduced any further. Or can it?

Stack and Private Data

Well, those are already insanely small... though I do wonder why they aren't even smaller. The program does not have anywhere near that much private data or stack, I think.

Can you suggest ways of making any of these sections smaller than they already are?


Further findings:

  • a blank no-CRT program uses about 204 KB
  • call to CreateWindow adds 420 KB
  • call to set the keyboard hook adds 156 KB
  • avoiding the use of CRT saves 20 KB
  • the total virtual size increases in a similar manner
  • not using the CRT saves quite a bit in EXE size: from 54 KB down to 18 KB, 12 of which are resources.

So it looks like most of this memory is consumed by Windows API, which seems to preclude significant further reductions, unless one can figure out a way to make the hook / tray icons work without creating a window (this program already ignores all messages anyway).

like image 233
Roman Starkov Avatar asked Jan 13 '12 17:01

Roman Starkov


2 Answers

It's possible to completely omit the C runtime library by relying on APIs provided by the OS (which are in DLLs you're already mapping into your process) or by implementing them yourself.

It's generally not worth it, unless you're already making minimal use of the language's runtime library. It also makes your application even less portable.

like image 192
Adrian McCarthy Avatar answered Sep 28 '22 03:09

Adrian McCarthy


The "Process Environment Block" contains a copy of all environment variables.

If you don't inherit any environment (i.e. the parent process spawned you with a clean environment) then that might yield a noticeable decrease, relative to the total memory usage you're looking at now.

like image 32
Ben Voigt Avatar answered Sep 28 '22 03:09

Ben Voigt