幸
幸福鸟
Unregistered / Unconfirmed
GUEST, unregistred user!
程序如下,在connect时建立线程,在EXCUTE时更新结构的“最后更新记录时间”,定时执行RefreshThreadList函数,将超过2分钟(2分钟为界面设置的)未更新数据的连接断掉。
现在的问题时,程序运行一段时间后,内存耗用很大,开始时是几M,随着连接的客户端增加(但超过时间的也断开了),后来就几百M了,即使连接数为0了,内存也没下来,也就是感觉有些东西没释放掉,看看是什么问题,哪里没释放掉呢???
procedure TfrmShowMain.ServerDisconnect(AThread: TIdPeerThread);
begin
try
except
end;
end;
procedure TfrmShowMain.ServerConnect(AThread: TIdPeerThread);
var
Client: PMyClient;
list: TList;
i: integer;
Ret: DWord;
begin
try
CoInitialize(nil); // 这个在线程中是必需的
Client := new(PMyClient);
Client.id := AThread.ThreadID; //GetTickCount + Random(1000);
Client.IP := AThread.Connection.Socket.Binding.PeerIP;
Client.Port := AThread.Connection.Socket.Binding.PeerPort;
Client.LoginTime := formatdatetime('yyyy-mm-dd HH:mm:ss', now);
Client.UpdateTime := formatdatetime('yyyy-mm-dd HH:mm:ss', now);
AThread.Data := Pointer(client); //< < 指到 athread的指针中
AThread.FreeOnTerminate := true; //20071101加
Client.MYQuery_local := tadoquery.Create(nil); //连接本地 创建
Client.MYQuery_local.Connection := connection_local;
AThread.Connection.Write('WELCOME');
client.CurMessage := ReturnLog(Client.IP, 'client connect!', Client.id);
AThread.Synchronize(AddThreadLogTxt);
except
on E: Exception do
begin
end;
end;
end;
procedure TfrmShowMain.RefreshThreadList;
var
i, j: integer;
list: TList;
Client: PMyClient;
filename: string;
count: integer;
t1, t2: tdatetime;
begin
if not server.Active then
exit;
try
List := Server.Threads.LockList;
for i := 0 to List.Count - 1 do
begin
try
Client := Pointer(TIdPeerThread(List.Items).Data);
if Client = nil then
continue;
//超过设置时间后断开
if ((now - strtodatetime(Client.UpdateTime)) * 24 * 60 * 60) > (60 *
seAutoClose.Value) then
begin
Client.MYQuery_local.Close;
FreeMem(Client);
try
TIdPeerThread(List.Items).Data := nil;
TIdPeerThread(List.Items).FreeOnTerminate:=true;
//TIdPeerThread(List.Items).Connection.ClearWriteBuffer;//执行这句也不行
TIdPeerThread(List.Items).Connection.Disconnect;
TIdPeerThread(List.Items).Stop;
except
on E: Exception do
begin
//TIdPeerThread(List.Items).Stop;
//AddErrorLogTxt(formatdatetime('HH:MM:SS', Time) + ' RefreshException:'
// + e.Message);
end;
end;
CoUnInitialize();
end;
except
end;
end;
finally
Server.Threads.UnlockList;
end;
end;
补充一下,每个线程创建一个adoquery,但连的是一个connection,用的是indy9.0.18
现在的问题时,程序运行一段时间后,内存耗用很大,开始时是几M,随着连接的客户端增加(但超过时间的也断开了),后来就几百M了,即使连接数为0了,内存也没下来,也就是感觉有些东西没释放掉,看看是什么问题,哪里没释放掉呢???
procedure TfrmShowMain.ServerDisconnect(AThread: TIdPeerThread);
begin
try
except
end;
end;
procedure TfrmShowMain.ServerConnect(AThread: TIdPeerThread);
var
Client: PMyClient;
list: TList;
i: integer;
Ret: DWord;
begin
try
CoInitialize(nil); // 这个在线程中是必需的
Client := new(PMyClient);
Client.id := AThread.ThreadID; //GetTickCount + Random(1000);
Client.IP := AThread.Connection.Socket.Binding.PeerIP;
Client.Port := AThread.Connection.Socket.Binding.PeerPort;
Client.LoginTime := formatdatetime('yyyy-mm-dd HH:mm:ss', now);
Client.UpdateTime := formatdatetime('yyyy-mm-dd HH:mm:ss', now);
AThread.Data := Pointer(client); //< < 指到 athread的指针中
AThread.FreeOnTerminate := true; //20071101加
Client.MYQuery_local := tadoquery.Create(nil); //连接本地 创建
Client.MYQuery_local.Connection := connection_local;
AThread.Connection.Write('WELCOME');
client.CurMessage := ReturnLog(Client.IP, 'client connect!', Client.id);
AThread.Synchronize(AddThreadLogTxt);
except
on E: Exception do
begin
end;
end;
end;
procedure TfrmShowMain.RefreshThreadList;
var
i, j: integer;
list: TList;
Client: PMyClient;
filename: string;
count: integer;
t1, t2: tdatetime;
begin
if not server.Active then
exit;
try
List := Server.Threads.LockList;
for i := 0 to List.Count - 1 do
begin
try
Client := Pointer(TIdPeerThread(List.Items).Data);
if Client = nil then
continue;
//超过设置时间后断开
if ((now - strtodatetime(Client.UpdateTime)) * 24 * 60 * 60) > (60 *
seAutoClose.Value) then
begin
Client.MYQuery_local.Close;
FreeMem(Client);
try
TIdPeerThread(List.Items).Data := nil;
TIdPeerThread(List.Items).FreeOnTerminate:=true;
//TIdPeerThread(List.Items).Connection.ClearWriteBuffer;//执行这句也不行
TIdPeerThread(List.Items).Connection.Disconnect;
TIdPeerThread(List.Items).Stop;
except
on E: Exception do
begin
//TIdPeerThread(List.Items).Stop;
//AddErrorLogTxt(formatdatetime('HH:MM:SS', Time) + ' RefreshException:'
// + e.Message);
end;
end;
CoUnInitialize();
end;
except
end;
end;
finally
Server.Threads.UnlockList;
end;
end;
补充一下,每个线程创建一个adoquery,但连的是一个connection,用的是indy9.0.18