Solved

Memory

Posted on 2003-11-26
8
336 Views
Last Modified: 2010-04-05
Hi,

Can anyone tell me how to create a program as memory efficient as possible.
Do you need to create all objects in code to do this?
Are there any more ways than, Free, FreeAndNil etc to release memory once
my program has finished with it.

I have a program that has a timer and every 10 seconds it runs code to process instructions on a database. I use query's to access the data and also to update my data. But as my program runs, which is 24/7, I can see the Mem Usage in Task Manager slowly accumulating over time. I create all my query's in code and free them up once im finished so I am struggling to think why this memory is accumulating over time.

I am using Delphi 5 Pro and my app is a standard windows exe.

Any help would be greatly appreciated
Thanks
0
Comment
Question by:HotRod40
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Expert Comment

by:lamtl354
ID: 9829397
hi,

you could:
use less Database object as posssibly
create object dynacmic instead of placing on form
free up (obh.free) after use.

hope helps
0
 
LVL 2

Expert Comment

by:Robn
ID: 9831885
Is it possible for this routine to take longer than 10 seconds to complete? If so, depending on how your code was written, you may be creating objects that are already created and then loosing the reference to them.

Just a thought.

You may also have a memory leak somewhere in your code that you may not see right now.
0
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 9857251
if you create objects with AOwner set to self you will not have a memory leak if you close
the application but during the runtime of the programm you will
experience incrementing memory usage

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:HotRod40
ID: 9873408
Gandalf,

Is there any other options to set AOwner to other than Self ?

Thanks
0
 
LVL 2

Expert Comment

by:Robn
ID: 9874235
I've seen code where a form is created with Application as the owner. I personally do not like this coding style. I also don't like to code objects that stick around for longer than they need to be.

However, I have seen some recent posts on EE where they talk about memory leaks in the Query objects (I'm not a DB guy so I'm not really sure if this applies to you). Your code may be correct and the problem may lie elsewhere.

Here are some links
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20815252.html
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20812633.html

Regards,
Rob
0
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 9880795
you can set the owner to nil

that also means you have to free the objects yourself.

but that also means that you can free the objects at the time you
want and you must not wait until the Owner is freed.

for instance:


procedure formcreate()
begin
  myPanel := TPanel.create(nil)
  mypanel.parent := Mainform;  { to show it somewhere }
...
end;

procedure Formdestroy();
begin
  myPanel.free;
 
...
end;


you can call the mypanel.free anywhere you like
but it also means if you forget it you have a memory leak after the programm is closed


0
 
LVL 2

Expert Comment

by:Robn
ID: 9882034
gandalf_the_white is correct, however to avoid any confusion, you can still create the panel with an owner and free it anytime you like before the owner is freed. When this happens, the owner will Remove that component from its internal list so that it knows not to free it when it is destroyed.

However, I still bet the leak isn't in your code but in the components you are using (then again, I haven't seen your code).

Regards,
Rob
0
 
LVL 3

Accepted Solution

by:
gandalf_the_white earned 150 total points
ID: 9903532
my point was that if you create components with Owner set to the form during runtime
they are not freed until the program is closed.

that means your memory usage will raise during the execution if you create ANY Components during runtime.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question