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:
CreateWindow
adds 420 KBSo 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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With