RTC Forums
November 24, 2024, 01:39:30 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Memory issue with DelphiDataSetToRtc  (Read 4889 times)
jorgen
RTC Expired
*
Posts: 20


« on: April 12, 2016, 12:48:18 PM »

I have this code:

class function TServer_Utils.JSONFromDataset(ADataset: TDataSet): String;
var
  rtcDS: TRtcDataSet;
  AHugeString: TRtcHugeString;
begin
  AHugeString := nil;
  rtcDS := TRtcDataSet.Create;
  try
    AHugeString := TRtcHugeString.Create;

    DelphiDataSetToRtc(ADataset, rtcDS); // This function is in the "rtcDB" unit
    rtcDS.to_JSON(AHugeString);
    Result := AHugeString.Get;
  finally
    // rtcDS.Clear;

    FreeAndNil(rtcDS);
    FreeAndNil(AHugeString);
  end;
end;


It is 800 rows in the dataset
It uses 10 mb of memory, but only the first time it is used,

Here is the memory use:
12 MB
  DelphiDataSetToRtc(ADataset, rtcDS);
18 MB
    rtcDS.to_JSON(AHugeString);
21 MB
    Result := AHugeString.Get;
23 MB
  FreeAndNil(rtcDS);
21 MB
    FreeAndNil(AHugeString);
21MB


Shouldn't it be possible to do this without any memory leak?

Jørgen
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: April 12, 2016, 12:58:09 PM »

How are you checking your Application's Memory Usage?

Best Regards,
Danijel Tkalcec
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #2 on: April 12, 2016, 01:44:05 PM »

By the way (not directly related to your memory question) ... if you just want a JSON String from a TRtcValueObject instance (like TRtcDataSet), there is no need to manually declare, create and free a local TRtcHugeString variable. You can simply use the "toJSON" method (or "toJSONEx" - if you want a RtcByteArray instead of a String).

In other words, your code example (from above) would look like this:

class function TServer_Utils.JSONFromDataset(ADataset: TDataSet): String;
var
  rtcDS: TRtcDataSet;
begin
  rtcDS := TRtcDataSet.Create;
  try
    DelphiDataSetToRtc(ADataset, rtcDS); // This function is in the "rtcDB" unit
    Result := rtcDS.toJSON;
  finally
    rtcDS.Free;
  end;
end;

Best Regards,
Danijel Tkalcec
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: April 13, 2016, 07:45:26 AM »

Check these DocWiKi Topics about Delphi Memory Management:
http://docwiki.embarcadero.com/RADStudio/XE8/en/Memory_Management
http://docwiki.embarcadero.com/RADStudio/XE8/en/Monitoring_Memory_Usage
http://docwiki.embarcadero.com/RADStudio/XE8/en/Configuring_the_Memory_Manager

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.025 seconds with 17 queries.