Link to home
Start Free TrialLog in
Avatar of xpher
xpher

asked on

System Resources

When I run my application the System Resources drop dramatically. If I start with System Resources of say 90% I run my app and the System Resources drop down to as low as 48%. This also happens while working on the app in Delphi IDE which causes obvious problems i.e the System Resources have dropped to 48% I try to run the app from within Delphi - BANG cpu errors hangs etc.

My app compiles to about 1.6MB there are a few images in there and a PageControl with around 12 TabSheets. Where am I going wrong with using so much System Resources? Any suggestions?
I'm using D4 Win98

Hopefully
Chris
Avatar of inthe
inthe

i dont think you got too much to worry as about my system resources are down to 43% with delphi open and only a button and a memo on the form..
closed outlook express got 47% now..
Avatar of xpher

ASKER

Mmmm trouble is I get problems with messages like can't draw on canvas (running app without Delphi open) and I haven't done anything as far as drawing or making changes to canvas, and then my machine starts to lock up. When you have Delphi open and System Resources at 47% can you actually run your app from Delphi with no problems? I ask cos' I wonder if there is a problem with my system, I've just installed a Rainbow Runner card and also upgraded from 64MB RAM to 128MB RAM.

Cheers
Chris

ps My worry is also if app is used on anpther machine it may cause problems.
Avatar of simonet
How are you calculating the system resources? WHat program are you using?

Alex
i have no trouble running apps etc from delphi ..
i dont believe my resource program anyway (microsoft one)
Did you have any trouble before the upgrade?
Avatar of xpher

ASKER

Hi Alex
I'm using Performance tab from Win98 System Properties. (Right click MyComputer etc.)

Regards
Chris
i'd get some decent programs like norton utilities or sisoft sandras to see what they say.
when using these i now have much better resources than with the win98 performance tab.

Chris,

I wouldn't worry about it. When you're working on the IDE, you have to remember that Delphi also takes up a lot of resources, including many hidden windows (forms).

However, there are tricks we can use to optimize resource usage:

1) Use TBevel instead of TPanel. I've seen many people using a TPanel when alll they want to do is to create a nice border effect. Use TPanel only if you want to create borders AND have a container for the components. TPanels use a lot more resources (including GDI resources) than TBevel.

2) Create your forms by hand. Don't let Delphi "auto-create" the forms for you (the default behaviour) you should create and free your own only forms when needed.

3) If possible create other objects in runtime too, and free them when no longer needed.

4) Do not waste variables. If you can optimize the usage of variables, you'll not only saving system resoruces, but also optimizing the overal performance of the application.

5) Do not inherit your components from a class whose features will not be needed. For example: suppose you're creating a new class that will only be created in runtime and is not visual. In this case, there's no need to inherit it from TComponent. Inherit from TPersistent or TObject, whichever lowest level best suits your need. The lower the hierarchy level of the "parent class", the best.

Other good sites to check, for both resource optimizations and performance optimzation are:

http://www.drbob42.com/delphi/perform.htm
http://www.econos.com/optimize/

Lots of info here: http://inner-smile.com/delphit.htm

Yours,

Alex
BTW, as of the images in your applications, I advise you to move them all to resources (check my site in order to know how to do that) and only load them when needed.

Yours,

Alex
Athena's Place: http://www.bhnet.com.br/~simonet
Avatar of xpher

ASKER

Hi Alex
Thanks for info. I have done much of what you say but can you enlighten me more on point 2)Create your forms by hand.

Cheers
Chris
What I mean was not to let all the forms in your project(s) be autocreated. See, when you add a form to a project, Delphi automatically adds code the the project source that makes that form be autocreated when the application is run. This way, when the application is run, it will take a lot longer to start, it will take up a lot more resources, and it's not code efficient.

What you must do is to create the forms only when you need them. Let me show you what I mean:

- Suppose I just created a new application in Delphi. I have Form1, which is my main form. Suppose I want to add another form, which will be called by form1 on the click of a button. If I do as Delphi suggests, my project source will look like this:

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

In the code above, Form2 will be create when the application is run, and all the memory it takes will be being consumed at all times, even when the form is not being shown or used.

the solution for the problem raised is to go to the Project Options and not let Form2 be an autocrated form. SO, whenver you need to use Form2, you can create it yourself, like this:

procedure TForm1.Button1Click(Sender : TObject);
begin
   if not assigned(Form2) then
     Form2 := TForm2.create(application);
   Form2.show;  // or showmodal... whichever fits your needs best
end;

Of course there are exception to this rule, but they are exceptions and experience will tell will in what cases an autocreated for is better than a runtime-created form

When working with runtime-created forms, make sure you dispose/free/release them when you're done using them (does this bring you memories from when you were in your early 20's ?! It does to me!)

Yours,

Alex
Avatar of xpher

ASKER

Hi Alex
Yes it does ;)

I will have alook at what you have just said.

Out of interest I have laid my hands on a Component Writers Guide for D1. In this book it says "..Windows doesn't know about graphic controls. They have no window handles, and therefore consume no system resources."

Cheers
Chris
That's perfectly right. Only descendants from TWinControl have handles. However controls that don't have handles are taking up other kinds of resources (like User resources).

Besides, if they have a canvas, they are also taking GDI resources.

Remember that there are 3 kinds of resources:
System, User and GDI

Yours,

Alex
Hi guys,

Alex is right with all his suggestions. Just one addition: If you have some bitmaps in your forms, try to put them into a TImageList. Each and every bitmap consumes 1 handle. One ImageList consumes 1 handle, too, doesn't matter how many bitmaps are in the ImageList.
If you want to see the resources all the time, you can download "showRes" from my homepage "http://beam.to/madshi". It simply opens a little stayOnTop window at the bottom right corner of the screen.
BTW, with Outlook and Internet Explorer and Delphi4 my resources are at 57%...

Regards, Madshi.
check out:
http://www.undu.com/helpmakers/samp_html/50500_winapi.html

File: FreeResources_1

Relation: [ WinApi ]
Size: 28 KB

Author: Jim Seach
 
Info: Freeing Windows Resources on the fly in Delphi

Avatar of xpher

ASKER

Well I am overwhelmed with all your help and ideas on this one, I am just sorry it is so little points but as with the rest of my life I'm broke.

Like the ShowRes 1.0 Madshi well useful.

Using Alex's idea of 'Create forms by hand' and a little re-designing the problem now seems to be solved. Running the app from within Delphi brings the System resources down to about 40%. If I run the app without Delphi the System resources drop from around 90%ish to around 72% which I don't think is too bad at all.

Would you like to post an answer Alex?

Once again thanks to all

Cheers
Chris :))
ASKER CERTIFIED SOLUTION
Avatar of simonet
simonet
Flag of Brazil 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