我的应用有问题。更长的运行时间= RAM中有更多MB。它在我尝试使用线程后开始。我使用带计时器的3倍踩踏。即时通讯使用即时通讯是因为即时通讯使用httpget,而没有它的话,GUI总是会在开始调用httpget时冻结,该下载文件大约为150字节。定时器间隔可以设置为5-300秒,大多数情况下使用5s。因此,每个计时器时间即时调用3x线程,并在每个线程即时调用httpget。但是应用吃内存可能是即时通讯在做错事情吗?也许他们以某种方式保持“开放”?
var
a, b, c: string;
type
TThread_1 = class(TThread)
private
FVar_1: string;
procedure Update_1;
protected
procedure Execute; override;
end;
TThread_2 = class(TThread)
private
FVar_2: string;
procedure Update_2;
protected
procedure Execute; override;
end;
TThread_3 = class(TThread)
private
FVar_3: string;
procedure Update_3;
protected
procedure Execute; override;
end;
procedure TThread_1.Execute;
begin
FVar_1 := HttpGet('url', 'a');
Synchronize(Update_1);
end;
procedure TThread_1.Update_1;
begin
a := FVar_1;
end;
procedure TThread_2.Execute;
begin
FVar_2 := HttpGet('url', 'b');
Synchronize(Update_2);
end;
procedure TThread_2.Update_2;
begin
b := FVar_2;
end;
procedure TThread_3.Execute;
begin
FVar_3 := HttpGet('url', 'c');
Synchronize(Update_3);
end;
procedure TThread_3.Update_3;
begin
c := FVar_3;
end;
procedure TForm1.Timer_thread(Sender: TObject);
var
t1 : TThread_1;
t2 : TThread_2;
t3 : TThread_3;
begin
if lb_1.IsChecked = True then
begin
t1 := TThread_1.Create(true);
try
t1.FreeOnTerminate := true;
finally
t1.Start;
end;
end;
if lb_2.IsChecked = True then
begin
t2 := TThread_2.Create(true);
try
t2.FreeOnTerminate := true;
finally
t2.Start;
end;
end;
if lb_3.IsChecked = True then
begin
t3 := TThread_3.Create(true);
try
t3.FreeOnTerminate := true;
finally
t3.Start;
end;
end;
end;
function HttpGet(const url: string; const tip: string): string;
var
HTTP: TIdHTTp;
begin
HTTP := TIdHTTP.Create(nil);
HTTP.Request.UserAgent := 'Mozilla/6.0 (compatible; Delphi)';
try
try
Result:=http.Get(url);
except
on E : Exception do
begin
if Pos('Error resolving Address', E.Message) <> 0 then
begin
if tip = '1' then Form1.lb_1.Text:='1';
if tip = '2' then Form1.lb_2.Text:='2';
if tip = '3' then Form1.lb_3.Text:='2';
end;
end;
end;
finally
FreeAndNil (http);
end;
end;
已编辑代码,但仍然占用内存(1MB / 1分钟-计时器间隔5秒),如果关闭计时器,则停止占用内存:
procedure TForm1.Timer_thread(Sender: TObject);
var
t1 : TThread;
t2 : TThread;
t3 : TThread;
begin
if lb_1.IsChecked = True then
begin
t1:= TThread.CreateAnonymousThread(
procedure
var s: string;
begin
s := HttpGet('url_a', 'a');
TThread.Synchronize(nil,
procedure
begin
a := s;
end);
end);
t1.Start;
end;
if lb_2.IsChecked = True then
begin
t2:= TThread.CreateAnonymousThread(
procedure
var s: string;
begin
s := HttpGet('url_b', 'b');
TThread.Synchronize(nil,
procedure
begin
b := s;
end);
end);
t2.Start;
end;
if lb_3.IsChecked = True then
begin
t3:= TThread.CreateAnonymousThread(
procedure
var s: string;
begin
s := HttpGet('c', 'c');
TThread.Synchronize(nil,
procedure
begin
c := s;
end);
end);
t3.Start;
end;
end;