如何编译以减少内存 /STACK 似乎没有改变任何东西?

发布于 2024-11-13 04:56:56 字数 523 浏览 4 评论 0原文

我创建了很多简单的程序,它们不需要任何内存,但它们总是在任务管理器的私有内存栏中显示大约1MB-1.6MB内存。

我读到与 link.exe 链接的默认堆栈大小是 1MB,我尝试像这样使用 /STACK :

/STACK:65536 (64kb)
/STACK:16777216 (16mb)

当我运行程序时,内存根本没有改变..

即使是像这样的简单程序,使用 1.6 MB 编译为 64 位,带有 link.exe,没有库(simple.c):

#include <stdio.h>

int main() {
  puts("hello world\n");
  getchar();
  return 0;
}

谁能告诉我如何减少简单程序的内存?我知道 1MB 并不多,但我很好奇,因为有一些 Windows 进程在 Taskman 中显示内存非常低,例如 smss.exe 正在运行 0.4MB 私有内存和 2 个线程。

谢谢!

I create a lot of simple programs, which don't need any memory, but they always show around 1MB-1.6MB memory in the private memory column in task manager.

I read that the default stack size is 1MB for linking with link.exe, i tried playing with /STACK like this :

/STACK:65536 (64kb)
/STACK:16777216 (16mb)

when i run the program, the memory hasn't changed at all..

Even a simple program like this, using 1.6MB compiled as 64bit with link.exe and no libs (simple.c):

#include <stdio.h>

int main() {
  puts("hello world\n");
  getchar();
  return 0;
}

Can anyone tell me how i can reduce the memory on simple programs? i know 1mb isn't much but i'm very curious as there are some windows processes which show very low memory in taskman, e.g. smss.exe is runing 0.4MB private memory with 2 threads.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-11-20 04:56:56

如果不包含 stdlib 并确保不链接到不使用的库,则可以减少占用空间。所有链接的 dll 都有自己的私有数据段,我相信它包含在您的私有数据集中。


使用 Yes (/NODEFAULTLIB) 删除所有 STD 库后,将我的优化设置为 O1 并设置我的入口点 winmain 以避免使用此代码出现 @_crtstatuperror

#include <windows.h>

int winmain(    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{

    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    char out[] = "bob";
    DWORD Outchar = 0 ;    
    WriteConsole(h, out, 4, &Outchar,  NULL);
    return 0;
}

我将内存使用量降至 292K,而没有变得非常奇特,我认为您不能降低很多

You can reduce your foot print if you don't include stdlib and make sure not to link in an libraries you don't use. All linked dlls get there own private data segment and I believe that is included in your private set.


After removing all STD libs using Yes (/NODEFAULTLIB) setting my optimizations to O1 and setting my entry point winmain to avoid the @_crtstatuperror using this code

#include <windows.h>

int winmain(    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{

    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    char out[] = "bob";
    DWORD Outchar = 0 ;    
    WriteConsole(h, out, 4, &Outchar,  NULL);
    return 0;
}

I got the memory usage down to 292K with out getting really exotic I don't think you could get it a lot lower

谎言 2024-11-20 04:56:56

您也可以尝试减小堆大小。对于 Visual Studio,可以通过 /HEAP 设置来完成此操作。 (默认情况下,堆为 1MB。)

You might also try reducing your heap size. For Visual Studio, this can be done with /HEAP setting. (By default, the heap is 1MB.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文