Is there someone in there that can explain why my code causes a memory leakage of 4KB, looking on the TaskManager. Delphi 2005, service application:
unit uMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
ExtCtrls;
type
TgwDebugService_s = class(TService)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServiceExecute(Sender: TService);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
gwDebugService_s: TgwDebugService_s;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
gwDebugService_s.Controller(CtrlCode);
end;
function TgwDebugService_s.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TgwDebugService_s.ServiceExecute(Sender: TService);
begin
// Service is Fired
while not Terminated do
ServiceThread.ProcessRequests(True);// wait for termination
end;
procedure TgwDebugService_s.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
// Service stops
end;
procedure TgwDebugService_s.Timer1Timer(Sender: TObject);
var
sl : TStringList;
begin
Timer1.Enabled := False;
Sleep(1000);
sl := TStringList.Create;
try
//if FileExists( 'c:\OnlyOnMyPc\test.txt' ) then
sl.LoadFromFile( 'c:\OnlyOnMyPc\test.txt' ); // remove this line and will be find
sl.Add( 'Test @ ' + FormatDateTime( 'dd.mm.yyyy hh:mm:ss.z', Now ) );
sl.SaveToFile( 'c:\OnlyOnMyPc\test.txt' );
finally
sl.Clear;
FreeAndNil( sl );
end;
Timer1.Enabled := True;
end;
end.
Appreciate your time and your help.
Cheers. gbp
Before looking for leaks in code, you first need to understand if you really have a leak or not. Task Manager shows what Windows knows about your application, and its number needs to be understood in the context of how Windows manages memory - then you have to understand how Delphi manages memory within an application.
To learn how to read Task Manager numbers, I'd suggest you the excellent Mark Russinovich's blog and videos:
http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/WCL405
He will explain you how actually memory allocation in Windows works (Delphi memory managers then suballocate it, but it still need to use Windows memory allocations calls), and will also explain many of his tools which allows to give a deeper look to many of the inner workings of Windows applications - beyond what FastMM can tell you.
There are also tools called "profilers" that can track memory allocation and deallocations and tell you which code allocates memory and later does't free it.
Otherwise you may chase leaks that actually don't exist...