how to compile to reduce memory /STACK doesn't seem to change anything?
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!
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
You might also try reducing your heap size. For Visual Studio, this can be done with /HEAP setting. (By default, the heap is 1MB.)
精彩评论