procedure IAmCalled*Very*FrequentlyToDoDBOperations;
procedure IAmCalled*Very*FrequentlyToDoDBOperations;
var
ms: TMemoryStream;
m1: TMemoryStream;
tmp: TMemoryStream;
...
begin
ms := TMemoryStream.Create;
m1 := TMemoryStream.Create;
tmp := TMemoryStream.Create;
try
// about a dozen lines of code that operate on streams
finally
ms := nil;
tmp := nil;
m1 := nil;
end;
// some condition
tmp.free;
end;
after running the app for about 3-5min. the app would have leaked around 2MB... more or less depending on what the user would have done with the app, but in a typical usage, it would have leaked thens of MB and in some corner cases, some streams would have been "freed" after "nil-ing".
var
ms: TMemoryStream;
m1: TMemoryStream;
tmp: TMemoryStream;
...
begin
ms := TMemoryStream.Create;
m1 := TMemoryStream.Create;
tmp := TMemoryStream.Create;
try
// about a dozen lines of code that operate on streams
finally
ms := nil;
tmp := nil;
m1 := nil;
end;
// some condition
tmp.free;
end;
after running the app for about 3-5min. the app would have leaked around 2MB... more or less depending on what the user would have done with the app, but in a typical usage, it would have leaked thens of MB and in some corner cases, some streams would have been "freed" after "nil-ing".
nil-ing your variable is not correct ! You must call Free.
ReplyDeleteThis is what happens when you first lern language with garbage collection and then try to program in real language ;)
ReplyDeleteMaciej Bąk you hit the nail right there, the code style indicated that he is a java dev mainly.
ReplyDeleteDorin Duminica ... or Objective-C
ReplyDeleteReal devs come from asm or C, the rest are plushies ;-)
ReplyDeleteAlexey Petushkov most likely java because the project is full of interfaces and every "get" and "set" method begins with "get" and "set"
ReplyDeleteP.S. lowercase "get" and "set"
ReplyDeleteEric Grange Real programmers dont worry about language they programming on. ;)
ReplyDeleteI started with Z80 asm before moving to 68K asm and finally x86 asm :)
ReplyDeleteI am used to cleaning up my own mess.
Z80 was a cool little chip
ReplyDeleteI started well before Z80! The first processor I programmed was a National Semiconductor SC/MP, alias SCAMP (http://en.wikipedia.org/wiki/National_Semiconductor_SC/MP). I used it in 1978. It ran at 1 MHz. There was no OS at all. Later I switched to the Intel 8080, then NSC800, the Z80, then Intel 8086. I started C language with Z80 but still done a lot of assembler for performance reasons. I also used a Motorola 68000 programmed in Pascal ! Then came the PC. You know where we are today :-)
ReplyDeleteWell, I started on Z80, first basic then asm (cpc 464, did some 8bit demo scene, when you had to sync with the CRT to squeeze extra colors, and when non-synthesized audio was done by hacking the buzzer with PWM, yes, the buzzer, not the fancy DACs that came later on). Also did some Z80 on specialized industrial hardware later on. Then mostly C/C++ across various hardwares and compilers, before finding Delphi 2.
ReplyDeleteFor bonus sad points, replace those with TFileStreams. Yup, seen that... Now your frequent DB operations are even further slowed down by unnecessary I/O.
ReplyDeletebegin
ReplyDeletems := TMemoryStream.Create;
m1 := TMemoryStream.Create;
tmp := TMemoryStream.Create;
try
// about a dozen lines of code that operate on streams
finally
ms.free;
tmp.free;
m1.free;
end;
// some condition
// tmp.free; should not free anymore.
end;
you could use freeandnil(ms)
ReplyDelete