Datasnap Memory Consumption

I am working on datasnap client server software. When executed, the server reads data from my database and stores it in memory. Each client, when connected to the server, requests data that invokes the procedure on the server. But I have problems with a huge amount of memory on the server side.

Server Life The cycle specified for a call or session does not affect the huge memory used by the server.

In this test, about 5 MB is used when starting the server. Connecting the client and receiving data from the server makes the server use another 58 MB. For every next customer! And this is just with 6 objects. On my real software server, the client receives more than 200 MB. My old version of the software has over 50 clients.

I tested this with Delphi XE6, XE7, XE8, 10, 10 with update 1. ReportmemoryLeakAtShutdown does not report anything about the client and a very small amount on the server, but reported as a known error.

Am I doing something really bad, or is it a Delphi problem?

Here is my test source:

common block for server and client:

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; 

server methods

 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; 

Client

 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; 

you can download sources here: https://kikimor.com/owncloud/index.php/s/Aw55lBzvFX9Q6tl

+5
source share

Source: https://habr.com/ru/post/1240418/


All Articles