如何避免在delphi中的这个循环中出现内存不足错误?

时间:2013-02-23 10:44:53

标签: delphi

我正在从filelistbox加载文件。当filelistbox中的文件号很大时,程序会提示“内存不足”。怎么解决?

提前致谢。

[编辑]

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

这是在Delphi 7上。我的问题是:

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?

1 个答案:

答案 0 :(得分:2)

  

List.clear方法是否清理内存?

是的,TStringList.Clear方法释放字符串分配的内存

  

stringlist中的项目是否有数量限制?

不,没有实际限制。

  

你能告诉我怎么解决吗?

不,因为不可能说代码

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

是否泄漏。