In a simple Delphi VCL application with one button on the form and the following OnButton event code:
procedure TForm1.Button1Click(Sender: TObject);
var
OpenDialog : TFileOpenDialog;
begin
OpenDialog := TFileOpenDialog.Create(nil);
try
OpenDialog.Options := OpenDialog.Options + [fdoPickFolders];
if not OpenDialog.Execute then
exit;
finally
OpenDialog.Free;
end;
end;
When I Execute the dialog my application memory usage more then doubles but after I OpenDialog.Free
that memory is not released.(I'm using ProcessExplorer to see how much memory my application is using)
How can I make it so that after I Free the object my memory usage returns back to what it was before I called the dialog?
This is normal operation. Memory managers typically don't return memory to the system and instead cache it later re-use. Further, the modules that are loaded the first time a file dialog is shown remain loaded in your process.
It's entirely possible that the system caches other resources to improve performance for subsequent uses of file dialogs.
This behaviour leads to better performance. Were you to be able to force the memory to be returned to the system, your program would perform more slowly.
Your code is correct. There is no leak. There is no problem for you to solve.