Warren
Warren

Reputation: 803

How to avoid Out of memory error in this loop in delphi?

I am loading files from filelistbox. When the file number in filelistbox is large, the program prompts "out of memory". How to solve it?

Thanks in advance.

[Edited]

public
...
List: Tstringlist;
...


procedure TForm1.FormCreate(Sender: TObject);
begin
  List:=tstringlist.create;
  List.sorted:=true;
end;

procedure TForm1.processfile;
var
  i:integer;
begin
  if filelistbox1.Count =0 then exit;
  for i:=0 to filelistbox1.count-1 do // 10000 files in filelistbox1
  begin
    memo1.clear;
    memo1.lines.loadfromfile(DirectoryListBox1+'\'+
      filelistbox1.Items.Strings[i]);  
    Addinlist;
    List.savetofile(afile);  
  end;
end;

procedure TForm1.Addinlist;

var
  w1: string;
  p1: tperlregex;
begin
  List.clear;
  p1:=tperlregex.Create(nil) ;
  p1.Subject:=memo1.text;
  p1.regex:='\w+';
  while p1.MatchAgain do 
  begin
    w1:=p1.MatchedExpression;
    List.add(w1);
  end;
  freeandnil(p1);
end;


Windows task manager memory displays: (approxomate number)

file number reaches 10, used memory 402M
file number reaches 100, used memory 402M
file number reaches 500, used memory 408M
file number reaches 1000, used memory 412M
file number reaches 1600, used memory 432M
file number reaches 2200, used memory 460M
file number reaches 3000, used memory 500M
file number reaches 5500, used memory 650M // prompts "Out of memory", form1 freez

This is on Delphi 7. My questions are:

Does List.clear method clean memory? 
Is there number limit to the items in stringlist?
Can you please let me know how to solve it?

Upvotes: 0

Views: 4986

Answers (1)

kludg
kludg

Reputation: 27493

Does List.clear method clean memory?

Yes, TStringList.Clear method frees memory allocated by strings

Is there number limit to the items in stringlist?

No, there is no practical limit.

Can you please let me know how to solve it?

No, because it is impossible to say does the code

List.clear;
...
...// parse each word in memo1;
List.add(eachwordinmemo1);

leak or not.

Upvotes: 2

Related Questions