PDA

View Full Version : About memory usage...



Mike
13-11-2002, 07:55 AM
When I create a simple application like:

program SmallApp;

function MessageBoxA(HWND: Cardinal; Msg,Title: pChar; Flags: Cardinal): Cardinal; stdcall; external 'User32.dll';

begin
MessageBoxA(0,'Hello from small app.','Small App',0);
end;

The executable is 6 kb, which is fine... But when I run the program, why an earth is takes 3 MB ram? This is as much as full VCL apllication uses when one form is created. For example Notepad takes only about 70 kb memory. Is there any way I can reduce memory usage?

Xorcist
13-11-2002, 08:19 AM
Not sure? Just guessing at a possible solution here, but did you unselect all the debug options before you compiled it? By the way what program are you using to monitor the memory usage.

Mike
13-11-2002, 08:35 AM
Yes, I have unselected all debug features and tried to compile with optimization on and off, but it still reserves huge amount of memory.

I also created program that doesn't use MessageBox, it only has infinite repeat loop and when I look in XP's task manager, the program have 31 handles created, but none of those handles are created by my code... There are also some GDI and User objects. I suppose System unit's initialization section has something to do with this.

I tried to make my own system unit, which doesn't contain anything than plain unit defination. I know that I can't use RTL procedures like Move, Delete etc with this unit. After I replaced the delphi's own system.dcu with my own, the compiler only gave me error message "Can't find unit system.pas" :(

Alimonster
13-11-2002, 09:15 AM
3 megs does seem a lot of memory for a messagebox, even under Windows...

Some of the handles you mentioned might be for things like the standard input/output that are set up in system.pas. I'll investigate that later today, since it's the kind of thing I enjoy.

I've had the same problem as you trying to get the RTL to compile. If you check in dcc32.exe, you'll see strings for various units - I think it's embedded into the compiler. If that's the case then things will get very annoying.

I think I might post on one of the Borland newsgroups later to see what the concensus is. It seems stupid to supply the RTL source but not let you change it!

Btw, what Delphi version and do you have the RTL source code?

Mike
13-11-2002, 11:18 AM
I have Delphi6 and I have all RTL sources, even system.pas. But system unit's code has some strange stuff in it and it's not compileable.

For exaple procedures such as Inc,Dec,Break,Continue etc. are not actually procedures because they are not called. "Inc" will eventually be compiled as "inc eax" or "add eax,?" CPU instruction if you look into CPU window.

Alimonster
13-11-2002, 11:38 AM
Yeah - IIRC, they're described as "compiler magic" functions. I think that the compiler directly copies the values, and it's hardcoded into dcc32.exe. There must be some stuff that can be removed - at the very least, some of the initialization section (which will get run behind-the-scenes). :?

Hey, a guy has to have hope, right?

TheLion
13-11-2002, 12:25 PM
Those functions are special indeed, I tried to use the same structure as used in the read function for optional parameters, but the compiler broke my try down too, so it seems they are treated differently by the compiler...

Mike
13-11-2002, 12:29 PM
I think it would be possible to change execution starting address in PE header, so that program starting point would point to statement that is right after "begin" in program's main unit. Then the system unit wouldn't allocate that 3 MB chunk.

Viro
13-11-2002, 05:41 PM
I think that this isn't something to be very concerned about. Why? The memory usage doesn't seem to scale very much as the application's size increases.

I've just written a test app that uses MSXML components, a Memo component, a couple of buttons, and a treeview. This program creates a DOM structure in memory, and displays structure in the treeview. All in all, it uses about 4.8 MB.

If I were you, I wouldn't be too concerned with a 3 MB memory footprint, but that may be because I come from a Java background :D

Mike
13-11-2002, 08:21 PM
You're right, it's not a big thing. But I'm just wondering why 7 kb program requires so much memory. For example if I want to write a program that runs in system tray and it's running in background all the time, it's very stupid to waste memory and decrease system performance. When the program could as well survieve with 20 kb.

Xorcist
13-11-2002, 11:19 PM
Um... I just compiled it in Delphi 5.0, and using the Resource Manager (under Win98SE), it appears it only takes up 1.2MB of memory. Wonder if it might also have something to do with running under NT/XP?

Alimonster
13-11-2002, 11:31 PM
Mine comes out as 992K of memory usage with Delphi 5 and Win2K. Still too much, mind you. I wonder how Delphi 6 affects things...

Xorcist
14-11-2002, 04:26 AM
What sort of tools does everyone use to monitor system resources and profile their applications? Just interested as I've never used any myself.

Mike
14-11-2002, 08:14 AM
I use only the XP's Task Manager, by default it doesn't show you how many handles or threads there are, but in View->Select Colums you can make it show various information, like how many I/O reads programs has done etc.

I installed Delphi3 and tried compiling the program: the executable came slightly bigger, it's 15 kb but the memory usage dropped to 616 kb. I think that's still too much for such a small app, but much less compared to program compiled with Delphi6.

Viro
14-11-2002, 08:49 AM
I've just tried compiling the program using Delphi 7. I got a 13.5 KB app, and the runtime foot print was around 2.4MB. I tried fiddling with compiler options, and it didn't change a thing!! That is so weird. I would have expected that turning of all the debugging options would make the exe size smaller, but nope, that didn't do a thing.

Armand
18-11-2002, 05:54 AM
When I create a simple application like:

snip..

The executable is 6 kb, which is fine... But when I run the program, why an earth is takes 3 MB ram?

Okay.. I made the same app with Delphi 6 {on a win 2k box}.

The app took up of 808K mem when running acording to task manager.
It was (9,216 bytes) but 16.0 KB (16,384 bytes) size on disk.
I ran it throught code seluth and didn't find any allocated memory at all.

Id love to know where the bloat is coming from.

Armand.

Delphic
18-11-2002, 10:50 PM
...

raidos
12-12-2002, 10:32 AM
Program SmallApp;

function MessageBoxA(HWND: Cardinal; Msg,Title: pChar; Flags: Cardinal): Cardinal; stdcall; external 'User32.dll';
function SetProcessWorkingSetSize(hProcess: LongWord; dwMinimumWorkingSetSize, dwMaximumWorkingSetSize: LongWord): LongBool; stdcall; external 'kernel32.dll';
function GetCurrentProcess: LongWord; Stdcall; external 'kernel32.dll';
procedure Sleep(dwMilliseconds: LongWord); stdcall; external 'kernel32.dll';

begin
MessageBoxA(0,'Hello from a small app.','Small App',0);
SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
MessageBoxA(0,'Hello from a smaller app.','Small App',0);
SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
Sleep(30000);
end.


Neat huh ? =)

Don't know exactly why it shrinks the memory footprint....i assume it swaps unused data to disk or something but atleast it appears that the application uses a minimal amount of memory.



Oh, and btw when needing a small application you should look into KOL
http://bonanzas.rinet.ru/


Regards
//raidos

Alimonster
12-12-2002, 11:44 AM
From the help at MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/setprocessworkingsetsize.asp):


If both dwMinimumWorkingSetSize and dwMaximumWorkingSetSize have the value -1, the function temporarily trims the working set of the specified process to zero. This essentially swaps the process out of physical RAM memory.

$FFFFFFFF = -1 (take 1, negate all bits, add one ;)). A handy thing to know if a parameter is expected to be unsigned and you have to pass in -1...