我正在研究datasnap客户端服务器软件。执行时,服务器从我的数据库中读取数据并将其保存在内存中。每个客户端在连接到服务器时,都会请求数据调用服务器上的过程。但是我遇到了服务器端大量内存消耗的问题。
设置为调用或会话的服务器lifeCycle不会影响服务器使用的巨大内存。
在此测试中,启动服务器时,它使用大约5MB。连接客户端并从服务器获取数据,使服务器使用另外58MB。对于每个下一个客户!这只是6个对象。在我真正的软件服务器中,为客户端获得超过200MB的空间。我的旧版软件有50多个客户端正在运行。
我用Delphi XE6,XE7,XE8,10,10和Update 1进行了测试。 ReportmemoryLeakAtShutdown在客户端报告任何内容,在服务器上报告的数量非常少,但报告为已知错误。
我做错了什么,或者是德尔福问题?
这是我的测试来源:
服务器和客户端的通用单元:
TOwnedFlag = (ofOwned);
TOwnedFlags = set of TOwnedFlag;
TMarshalList<T: class> = class
private
FList: TArray<T>;
FFlags: TOwnedFlags; // use set for internal flags because sets are not marshalled
public
constructor Create(AList: TArray<T>; AOwnsItems: Boolean = True); overload;
constructor Create; overload;
destructor Destroy; override;
property List: TArray<T> read FList;
end;
TMyClass = class
ID: integer;,
Name: String;
Desc: String;
Desc1: String;
Desc2: String;
Desc3: String;
....
constructor Create;
end;
implementation
constructor TMarshalList<T>.Create(AList: TArray<T>; AOwnsItems: Boolean);
begin
FList := AList;
if AOwnsItems then FFlags := [ofOwned];
end;
constructor TMarshalList<T>.Create;
begin
FFlags := [ofOwned];
end;
destructor TMarshalList<T>.Destroy;
var
LItem: T;
begin
if ofOwned in FFlags then
for LItem in FList do
LItem.Free;
inherited;
end;
constructor TMyClass.create;
var
i: integer;
begin
//this I made just to have objects with noticeable size...
desc := '01101011010110101010101010101010110101010100110100101010010101010101001010101010101010010100100010001111011010010101010110101101'+
'01101011010110101010101010101010110101010100110100101010010101010101001010101010101010010100100010001111011010010101010110101101'+
'01101011010110101010101010101010110101010100110100101010010101010101001010101010101010010100100010001111011010010101010110101101'+
'01101011010110101010101010101010110101010100110100101010010101010101001010101010101010010100100010001111011010010101010110101101';
for I := 1 to 8 do
desc := desc + desc;
desc1 := desc;
desc2 := desc;
....
end;
ServerContainer
MyList: TList<TMyClass>;
procedure TServerContainer.DataModuleCreate(Sender: TObject);
var
c: TMyClass;
I: Integer;
begin
MyList := TList<TMyClass>.create;
for I := 0 to 5 do begin
c := TMyClass.create;
c.ID := i;
c.Name := inttostr(i);
MyList.Add(c);
end;
end;
procedure TServerContainer.DataModuleDestroy(Sender: TObject);
var
I: Integer;
begin
for I := 0 to myList.Count-1 do
myList[i].Free;
myList.Free;
end;
ServerMethods
function TServerMethods.getMarshalList: TMarshalList<TMyClass>;
var
c: TMyClass;
I: Integer;
l: TList<TMyClass>;
begin
l := TList<TMyClass>.create;
for I := 0 to servercontainer.MyList.Count-1 do begin
c := TMyClass.create;
c.ID := servercontainer.MyList[i].ID;
c.Name := servercontainer.MyList[i].Name;
l.Add(c);
end;
Result := TMarshalList<TMyClass>.Create(l.ToArray, True);
l.free;
GetInvocationMetaData.CloseSession := True;
end;
客户端
procedure TForm1.Button1Click(Sender: TObject);
var
server: TServerMethodsClient;
c: TMyClass;
I: Integer;
list: TMarshalList<TMyClass>;
begin
ticks := GetTickCount;
memo1.Lines.clear;
server := TServerMethodsClient.Create(SQLConnection1.DBXConnection);
list := server.getMarshalList;
for i := 0 to high(list.List)-1 do begin
c := list.List[i];
end;
server.Free;
end;
您可以在此处下载资源:https://kikimor.com/owncloud/index.php/s/Aw55lBzvFX9Q6tl