Jump to content
NecoArc

Memory leak on parsing json

Recommended Posts

I have a simple function where i call a json file from memory and then parse it on a FMX project


 

procedure TPrincipal.Button3Click(Sender: TObject);
var
   jsonstr:string;
   json :TJsonObject ;
begin

    jsonstr:=TFile.ReadAllText(ExtractFilePath(paramstr(0))+'\jsonTeste.json', TEncoding.UTF8);
    json :=TJSONObject.ParseJSONValue(jsonstr) as TJSONObject;
    freeandNil(json);

end;



the json is a 449kb file so it's n ot a big one 

but when i execute the  TJSONObject.ParseJSONValue(jsonstr) as TJSONObject command
the memory of my aplication goes up in 20mb 
then when i execute freeandNil(json) the memory do not goes down
those 20mb continues to weigh throughout the execution of the application
when i close it, it present memory leaks on jsonObjects, jsonStrings, unicodeString etc.  

but like 

why????? i don't understand what is wrong and this seems to happend only with this json 

thanks for any help, the json file is attached.
 

jsonTeste.json

Edited by NecoArc

Share this post


Link to post
Just now, Lajos Juhász said:

I cannot reproduce the leak using Delphi 12.2

i'm also using delphi 12.2

did you use the same json file? notice that the memory goes up 20mb after parsing?

Share this post


Link to post
Just now, Lajos Juhász said:

I cannot reproduce the leak using Delphi 12.2

oh I forgot to mention that it's an FMX project I don't know if the same happens with VCL

Share this post


Link to post

It does not in my case using Task manager.

 

Before the string is read the working set is 3,752K. Read the string: 4,652K, parsing the json 7,280K, freeandnil - 4676K, when the click method is finished the memory set is back at 3,660K.

Share this post


Link to post
11 minutes ago, Lajos Juhász said:

I cannot reproduce the leak using Delphi 12.2

you helped me a lot bro, i created another project and realise the problem wasn't happening 

then, after some search i realise that it was because FASTMM4 is active on the project 

Edited by NecoArc

Share this post


Link to post
Just now, NecoArc said:

then, after some search i realise that it was because FASTMM4 is active on the project 

That alone should not be creating memory leaks.  Just having FastMM enabled should not be accounting for a 20MB increase in memory usage.  And, although FastMM does cache freed memory for later reuse, which can appear as a leak from Task Manager's POV but really isn't, it should not be reported as a leak during process shutdown.  Only real leaks (ie unfreed allocations) should be reported as leaks.

 

Can you provide the actual leak report?  And, do you have tracing enabled so the report shows details about the code that allocated the leaked memory?

Share this post


Link to post

oh the irony, the lib used to find memory leaks is causing memory leaks

Edited by NecoArc

Share this post


Link to post
Just now, Remy Lebeau said:

That alone should not be creating memory leaks.  Just having FastMM enabled should not be accounting for a 20MB increase in memory usage.  And, although FastMM does cache freed memory for later reuse, which can appear as a leak from Task Manager's POV but really isn't, it should not be reported as a leak during process shutdown.  Only real leaks (ie unfreed allocations) should be reported as leaks.

 

Can you provide the actual leak report?  And, do you have tracing enabled so the report shows details about the code that allocated the leaked memory?

In my understanding, FASMM shouldn't cause an increase of 20mb either, but that's the best guess because when I remove the FASMM call from the .dpr of my project the situation stops happening.

i attached the memory leak report

AutoatendimentoRefatorado_MemoryManager_EventLog.zip

Share this post


Link to post
8 minutes ago, NecoArc said:

In my understanding, FASMM shouldn't cause an increase of 20mb either, but that's the best guess because when I remove the FASMM call from the .dpr of my project the situation stops happening.

i attached the memory leak report

AutoatendimentoRefatorado_MemoryManager_EventLog.zip

Before freeing the TJSONObject, can you verify that all of its child objects are set to Owned=True?  Also, do you have the same problem if you try with FASTMM5 instead of FASTMM4?

Share this post


Link to post
14 hours ago, NecoArc said:

oh the irony, the lib used to find memory leaks is causing memory leaks

No, FastMM is not causing leaks, you have not identified the problem yet.

 

The first thing to do is stop looking at task manager. Use the leak reporting mechanism to identify leaks.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×