#1 2017-11-10 13:30:07

Rahul
Member
Registered: 2017-11-10
Posts: 4

TDynArrayHashed goes outof memory

I have written some code which adding and finding records on timer interval, but after some time i am getting an out of memory exception.
Sample Code:

type
  ppckt = ^TPacket;
  TPacket = packed record
    Source     : string;
    Destination: string;
    Sport      : string;
    Dport      : string;
  end;
  TPackDynArray = array of TPacket;
   APackt           : TDynArrayHashed;
    Packts           : TPackDynArray;
    PacktsCount : integer;
    Packt             : ppckt;



procedure TForm1.FormCreate(Sender: TObject);
var
   i :integer;
begin
    Try
      APackt.Init(TypeInfo(TPackDynArray),Packts,nil,nil,nil,@PacktsCount);
      for i := 0 to 1000000 do
      begin
        new(packt);
        Packt.Source      := 'Source:'+inttostr(i);
        Packt.Destination := 'Destination:'+inttostr(i);
        Packt.Sport       := 'Sport'+inttostr(i);
        Packt.Dport       := 'Dport'+inttostr(i);
        APackt.Add(Packt^);
      end;
      Timer1.Enabled := True;
    Except
    End;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  indx   : integer;
  SNumbr : String;
begin
    Randomize;
    SNumbr            :=  IntToStr(RandomRange(100000,10000000));
    Packt.Source   :=  'Source:'+SNumbr;
    APackt.ReHash;
    indx := APackt.FindHashedAndFill(Packt^);
    if indx < 0 then
    begin
      new(packt);
      Packt.Destination  := 'Destination:'+SNumbr;
      Packt.Sport            := 'Sport'+SNumbr;
      Packt.Dport            := 'Dport'+SNumbr;
      indx                         := APackt.Add(packt^);
      Memo1.Lines.Add('Updated :'+inttostr(indx));
    end
    else
      Memo1.Lines.Add('Existed :'+inttostr(indx));
end;

Is it anything wrong with this code? How can I Proceed?

Last edited by Rahul (2017-11-10 13:33:25)

Offline

#2 2017-11-10 14:17:30

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,534
Website

Re: TDynArrayHashed goes outof memory

Hm... Did you try to remove Memo1.Lines.add ?

Offline

#3 2017-11-10 16:08:46

Rahul
Member
Registered: 2017-11-10
Posts: 4

Re: TDynArrayHashed goes outof memory

Ye I did..But It's not becz of Memo... Is it Some memory leak issue with  TDynArrayHashed?

Offline

#4 2017-11-10 17:26:19

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TDynArrayHashed goes outof memory

We run all regression tests with memory leak reporting, and it includes a lot of TDynArrayHashed process.
No leak reported so far.

Offline

#5 2017-11-13 09:38:40

Rahul
Member
Registered: 2017-11-10
Posts: 4

Re: TDynArrayHashed goes outof memory

just check with above code you will definitely get out of memory exception.

Offline

#6 2017-11-13 12:18:48

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TDynArrayHashed goes outof memory

You are adding infinitely items to a list, so a out of memory is expected.

Also calling Memo1.Lines.Add() is much more resource-demanding than TDynArrayHashed.

Offline

#7 2017-11-13 12:30:43

Rahul
Member
Registered: 2017-11-10
Posts: 4

Re: TDynArrayHashed goes outof memory

I removed memo from my application and I have set another timer to delete items from TDynArrayhashed after perticular interval but memory taken by my application continuously increased.

Offline

#8 2017-11-13 12:45:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TDynArrayHashed goes outof memory

Your TForm1.Timer1Timer code is leaking memory, for sure.

APackt.Add() is making a copy of the record into the array, AS DOCUMENTED.
So your new(packt) instance is never released.

Don't play with new(packt).
Just use a local stack-allocated variable.

Offline

Board footer

Powered by FluxBB