Link to home
Start Free TrialLog in
Avatar of bengore
bengore

asked on

Make small .exe with little memory usage

If i compile a very little program like:

uses Windows;
begin
  Messagebeep(0);
end.

and then I study the exe file with ResourcenWorkshop or ShowDeep (which shows a list of the used WinAPI function of the .exe) then I see: Delphi has linked a lot of unused stuff to the .exe.
I know every Delphi program loads the Ole32.dll into the memory. That means: the little messagebeep() program will needs more than 1MB memory!

How to make small .exe (without unused resource like MAINICON, string table, Bitmaps..) which needs only little memory (without loading unused DLL's)? Should I start to learn VisualC :-( ?
Avatar of philipleighs
philipleighs

Have you tried only declaring the functions you need?

Instead of
uses Windows;

Try
function MessageBeep(uType: Integer): Boolean; stdcall; external 'user32.dll';

Cheers,
Phil.

Sorry Ben, there is nothing you can do about it. When you use Delphi's components the VCL (Visual Component Library) will be linked to your app. This is what you see in ResourceWorkshop.

Epsylon.
Epsilon,

When you compile

program SmallExe;
uses Windows;
begin
  Messagebeep(0);
end.

It only includes 72 bytes of resources! The exe is 16kb.

You only get the VCL if you add Forms or Graphics or something to the uses clause.

Cheers,
Phil.

Yep, Phil is right. I've written some Delphi programs with only 16KB, too...   :-)
ASKER CERTIFIED SOLUTION
Avatar of ahalya
ahalya
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ben,

I was trying to find an article I have from Delphi Informant which actually explains in great detail why Delphi compiles such large EXEs compared to C++ (smaller than VB in total though!).

The reason Delphis EXEs are so much bigger than, say C, is because you do not have to create any low-level code such as message handlers.  You dont have to create your Windows in code - this is all done for you.  Basically, you are totally spoon fed your code - you dont have to really know anything to make an App in Delphi.

You can, if you are game enough, create your own event and message handlers, create all your own components from APIs or Windows supplied DLLs, design all your forms.  This will greatly reduce the size of your EXEs.  However, you will loose your RAD (Rapid Application Development).  You will basically be going back and writing Pascal like we used to in the days of TPW or OWL (and didnt we all totally enjoy that!!!).

The only way you can reduce your exe size, and still maintain the development environment we have with Delphi is to only use units you need, optimize your code, reuse code and components, stick as much as possible (especially graphics) into resource DLLs/DLLs, optimize your compiler settings for size and finally, if you are game, compress your exes.

I think the size of Delphi executables are fine considering all the hard work and time it saves me in the development stage.  Whats a few hundred Ks these days when most of us have 10+ gig hard drives and ISDN internet connections (or even 56K modems).

I'll put it this way: ITS A SMALL PRICE TO PAY FOR YOUR SANITY! :)


Hey Madshi,

Remember the other day where you spotted that I quoted your code?

Well ahalya has just quoted mine (albeit from C -> pascal)!

PS: I think the issue here is the amount of memory the app uses, not the size of the exe.

Cheers,
Phil.

Hehe, good code spreads itself...   :-)
Right on!  :-)
Avatar of bengore

ASKER

Thanks for all your answers and comments!
Please run my example and study it with the task manager of WinNT:

program test;
uses Windows;
begin
  FreeLibrary(GetModuleHandle('OleAut32')); {is this wrong? an error occured when closing the program (runtime error 216)}
  MessageBox(0,'Now switch to the WinNT Taskmanager!', 'Test',mb_Ok);
end.

If you compile it with Delphi (2/3/4) then it needs 1.5 MB memory!! That is to much! How can I reduce the memory usage?
I have to write a program which run in the taskbar area. My competitor/revial have write a similar program in VisualC and his .exe needs only 75 KB memory!
Thank you for your help!
Have you tried Phil's suggestion from

Wednesday, June 30 1999 - 05:00PM

?
Phil:

>  "Well ahalya has just quoted mine (albeit from C -> pascal)! "

When I started my WinAPI programming I asked questions on the net about the usage of the "CreateWindowsEx" function & its arguments.  In my code I might have used the examples from the net to create my Window.

May be you gave the answer for my CreateWindowsEx question (possibly in comp.lang.pascal.delphi.misc) but I'm sorry i don't recall who did.  

I didn't think that I have to state that I leaned to use this WinAPI functions from so and so, everytime i use WinAPI functions.

Further when i tell about a WinAPI function to someone, I don't expect then to credit me for its use in their answers.  (I hope you'll agree with me here: Hey, we all learned our WinAPIs someplace, didn't we ?)

I think I gave the reference of "RegisterServiceProcess" function to Madshi -in Q10088342- and when he gives that same function as the answer to someone elses question later -Q10109240- he was perfectly ok in not telling, from where he found the usage of the function.


Hey take it easy ahalya!

My comment was to Madshi.
I didn't mean to imply that you owe me credits or anything.

After all, it is called E-E for a reason!

BTW, the Q is:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10089658 

Hi ahalya,

you're right. The situation was this: In a different question (one day ago) Phil posted some code (a bit more complex code) from me. And he wrote that it was not his code, but that he didn't remember who had written it. So I only wrote something like: "It was me... :-)". That's why Phil in this question said that you used HIS code. He said it more to me (because of the other question I mentioned) not to you. So none of us has any problems with using code from other experts...   :-)
Hmm. However, if I copy quite complex code (more than 20 lines or so) from another expert I usually write his name to the code, only to be polite. Of course that's no must. I did that not with the RegisterServiceProcess you gave me, because it was "only" the API description - though *VERY* helpful for me...

Please take it easy, guyz!   :-)

Regards, Madshi.
Hi Madshi &Phil:

Thanks for your cool responses.  I'm sport :-)

I agree with Madshi that RSP is a wonderful function (I found it in  comp.lang.pascal.delphi.misc)

Cheers,



Good vibes guys.  :-)