当前位置:  开发笔记 > 编程语言 > 正文

Datasnap内存消耗

如何解决《Datasnap内存消耗》经验,为你挑选了0个好方法。

我正在研究datasnap客户端服务器软件.执行时,服务器从我的数据库中读取数据并将其保存在内存中.每个客户端在连接到服务器时,都会请求数据调用服务器上的过程.但是我遇到了服务器端大量内存消耗的问题.

服务器lifeCycle设置为调用或会话不会影响服务器使用的巨大内存.

在此测试中,启动服务器时,它使用大约5MB.连接客户端并从服务器获取数据,使服务器使用另外58MB.对于每个下一个客户!这只是6个对象.在我真正的软件服务器中,为客户端获得超过200MB的空间.我的旧版软件有50多个客户端正在运行.

我使用Delphi XE6,XE7,XE8,10,10和Update 1进行了测试.ReportmemoryLeakAtShutdown在客户端报告任何内容,在服务器上报告的数量非常少,但报告为已知错误.

我做错了什么,或者是德尔福问题?

这是我的测试来源:

服务器和客户端的通用单元:

TOwnedFlag = (ofOwned);
TOwnedFlags = set of TOwnedFlag;

TMarshalList = class
private
  FList: TArray;
  FFlags: TOwnedFlags; // use set for internal flags because sets are not marshalled
public
  constructor Create(AList: TArray; AOwnsItems: Boolean = True); overload;
  constructor Create; overload;
  destructor Destroy; override;
  property List: TArray read FList;
end;

TMyClass = class
  ID: integer;,
  Name: String;
  Desc: String;
  Desc1: String;
  Desc2: String;
  Desc3: String;
  ....
  constructor Create;
end;

implementation
constructor TMarshalList.Create(AList: TArray; AOwnsItems: Boolean);
begin
  FList := AList;
  if AOwnsItems then FFlags := [ofOwned];
end;

constructor TMarshalList.Create;
begin
  FFlags := [ofOwned];
end;

destructor TMarshalList.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;

procedure TServerContainer.DataModuleCreate(Sender: TObject);
var
  c: TMyClass;
  I: Integer;
begin
  MyList := TList.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;
var
  c: TMyClass;
  I: Integer;
  l: TList;
begin
  l := TList.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.Create(l.ToArray, True);
  l.free;
  GetInvocationMetaData.CloseSession := True;
end;

客户

procedure TForm1.Button1Click(Sender: TObject);
var
  server: TServerMethodsClient;
  c: TMyClass;
  I: Integer;
  list: TMarshalList;
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

推荐阅读
大大炮
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有