我可以进一步减少“私有字节”内存使用这个微小的MSVC ++项目? [英] Can I further reduce "private bytes" memory usage of this tiny MSVC++ project?

查看:216
本文介绍了我可以进一步减少“私有字节”内存使用这个微小的MSVC ++项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是出于好奇而不是真正的需要,但是这个微小的MSVC ++程序的内存使用可以进一步减少吗? BitBucket上的源文件



程序是用优化代码大小编译的。它创建一个消息窗口并设置一个键盘钩子,显示托盘图标以响应Caps / Num / Scroll Lock键按下。



根据VMMap,字节分配如下:

  260 KB:Image 
252 KB:Heap
240 KB:表
24 KB:堆栈
24 KB:私人数据
------
800 KB TOTAL

图片



应用程式本身只使用20 KB;其余的由十几个DLL消耗。


$ b

只在堆上分配大约3 KB的数据:某个类的三个实例。其余必须来自CRT和/或标准操作系统代码。



这可以减少吗?



总计:

页表这个程序的虚拟大小是44 MB,大约11k页。这是平均每页22个字节(虽然大概一堆条目搁置未使用)。所以这可能不能再减少。


$ b

疯狂小...虽然我不知道为什么他们甚至更小。



你能建议一些方法,使这些部分比他们已经更小吗?

p>




更多发现:




  • a空白无CRT程序使用大约204 KB

  • 调用 CreateWindow 添加420 KB

  • 调用设置键盘钩子添加156 KB

  • 避免使用CRT节省20 KB

  • 总虚拟尺寸以类似方式增加

  • 不使用CRT在EXE大小上节省了相当多的空间:从54 KB下降到18 KB,其中12个是资源。



所以看起来这个内存的大部分是由Windows API消耗,这似乎排除了显着进一步减少,除非能找到一个方法使挂钩/托盘图标工作,而不创建一个窗口这个程序已经忽略了所有的消息。)

解决方案

可以通过依赖于提供的API完全省略C运行时库OS(在DLL中,您已经映射到您的进程)或通过自己实现它们。



这通常不值得,除非你已经尽量少使用语言的运行时库。它也使您的应用程序更不便携。


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).

解决方案

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.

这篇关于我可以进一步减少“私有字节”内存使用这个微小的MSVC ++项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆