 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
warren.bare@gmail.com Guest
|
Posted: Mon Jan 16, 2006 8:59 pm Post subject: Simple memory leak |
|
|
Hi Folks,
I reproduced a very simple memory leak problem below. Basically, I
have a global TStringList object. Periodically I make a new string
list, then free the global object and point the global object at the
newly created string list.
This leaks memory.
Check it out in the simple example below. All you need is a form with
a timer on it that calls the routine every few seconds. My my real
app, the does not happen as often, but the principle is the same.
When you run this, you will see the task manager memory usage go up by
a little bit every time the function fires.
Memory experts please help. What's the deal here???????????
Thanks,
Warren
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
slGlobal: TStringList;
implementation
uses
Math;
{$R *.dfm}
procedure TForm1.Timer1Timer(Sender: TObject);
var
slTmp: TStringList;
begin
slTmp:= TStringList.Create;
while slTmp.Count < 10000 do
begin
// simulate a data string with random text
slTmp.Add(
StringOfChar(Chr(RandomRange(32,64)), RandomRange(10,30)) +
StringOfChar(Chr(RandomRange(32,64)), RandomRange(10,30)) +
StringOfChar(Chr(RandomRange(32,64)), RandomRange(10,30))
);
end;
// If i Just free slTmp, there is not leak, but if I free
// the slGlobal, then point slGlobal at slTmp, I get a leak
slGlobal.Free;
slGlobal := slTmp;
end;
end.
|
|
| Back to top |
|
 |
Nicholas Sherlock Guest
|
Posted: Mon Jan 16, 2006 9:37 pm Post subject: Re: Simple memory leak |
|
|
[email]warren.bare (AT) gmail (DOT) com[/email] wrote:
| Quote: | When you run this, you will see the task manager memory usage go up by
a little bit every time the function fires.
|
This is NOT a memory leak. The task manager is not the place to look to
find out memory usage. If you minimize your application, you'll see your
"memory usage" drop dramatically, Ie: The value that task manager
provides is useless.
(Note that your code will leak 1 stringlist on program close...)
Cheers,
Nicholas Sherlock
|
|
| Back to top |
|
 |
warren.bare@gmail.com Guest
|
Posted: Mon Jan 16, 2006 9:48 pm Post subject: Re: Simple memory leak |
|
|
Hmmmm. OK, I did not know that about the task manager at all. That
probably explains some other weirdness i have seen also.
Sorry for asking what may seem like a glaringly obvious question, but
how do I monitor real memory usage.
|
|
| Back to top |
|
 |
Nicholas Sherlock Guest
|
Posted: Mon Jan 16, 2006 10:03 pm Post subject: Re: Simple memory leak |
|
|
[email]warren.bare (AT) gmail (DOT) com[/email] wrote:
| Quote: | Hmmmm. OK, I did not know that about the task manager at all. That
probably explains some other weirdness i have seen also.
Sorry for asking what may seem like a glaringly obvious question, but
how do I monitor real memory usage.
|
It's not obvious at all, so don't worry . There are several tools for
Delphi that let you check for memory leaks. If you use the FastMM4
memory manager, for instance, performance of your applications is much
improved, memory usage is lowered, and you are informed about memory
leaks when your program closes.
I seem to remember tools called MemCheck and MemProof which will also help.
Cheers,
Nicholas Sherlock
|
|
| Back to top |
|
 |
etherington19@aol.com Guest
|
Posted: Tue Jan 17, 2006 11:17 am Post subject: Re: Simple memory leak |
|
|
Hi Warren B
I've used the following bit of code to track memory whilst developing.
PosMessage is simply a note of where the app was when I called this
proc. I think I found this stuff on About Delphi.
procedure LogMemory(PosMessage: string);
var
MS: TMemoryStatus;
begin
MS.dwLength := SizeOf(MemoryStatus);
GlobalMemoryStatus(MS);
Form1.EventLog(Format('%8s %8s %-s', [DateToStr(Date),
TimeToStr(Now),
PosMessage + ' ' + FormatFloat('#,###" KB"', MS.dwAvailPhys div
1024)]));
end;
Ron Etherington
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|