views:

200

answers:

3

Hello All,

I have been a dabbling into delphi off and on for years and I have always wondered why does minimizing and restoring an application cause it to use less memory ?

As an example I am using delphi 7 and I create a new project with nothing on it but the blank form all projects start out with and then I press F9 to run the application and then look at the memory usage for the app and it's sitting at around 3.5mb I then minimize the app and the memory usage goes down to around 760kb and then I finally restore the app and the memory usage goes back up to around 1.5mb which is roughly 1/2 of what it was when it first loaded and this has always confused me as to what is making this happen and even more to the point is there anyway to start the application with some directive that makes it use the 1.5mb of memory instead of the 3.5mb it normally uses.

Cheers, Dave

+5  A: 

probably is something with windows memory management. try the same thing with the windows calculator and the behavior is the same :))

here an answer: http://support.microsoft.com/?kbid=293215 and some folks have the same question: http://digital.ni.com/public.nsf/allkb/9EA3D4258E037B8A8625763300434D4D

best regards,

Radu Barbu
Thanks for the reply I was more curious than anything else at least now I know it's not just my apps are not the only ones doing this. Thanks Again,Dave
dave
+1 for KB article
Alexander
This is a good example of Windows where I usually say, when I figure out what they did: "[insert loud imprecatory statement here] I can't believe they did that!" Windows kind of lies to you, because they couldn't figure out which of the things they could tell you is closest to the thing that you want to know, like how much memory is this app using. So they picked one of the things they do know how to calculate, and labeled it with a user-friendly but inaccurate short label; "this is the one you're looking for", when clearly, it isn't.
Warren P
+1  A: 

Here you can find a very clear explanation from Ian Martins. When the application minimizes the system call SetProcessWorkingSetSize procedure for free inactive memory of process.

You can do the same adding this code to your application. In a button OnClick you can do this:

procedure LiberarMemoria;
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
    SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
end;

The effect is similar to minimize the application. If your application do some task thah eventually use a big block of memory, you can force it to free after use it, using this small code.

Regards

Neftalí
It would have helped me if the very clear explanation from Ian Marteens had been in English...
Marjan Venema
Quote from specified link: "The easiest thing would run from a timer, every minute, for example". Yeah, right. Stay away from this article for your own sake.
Alexander
@Alexander please, check and read my comment carefully and see that anywhere I have recommended that release of memory is made every minute.I said:"...If your application do some task thah EVENTUALLY use a big block of memory, you can force it to free AFTER USE IT"(1) Eventually(2) AFTER USE IT, not Every minute.I have recomended it, for the explanation and for the code. Please, read carrefully the responses.P.D: Excuse for mistakes with english.
Neftalí
@Marjan Venema, this article explain tha API that is called when you minimized one application; The result of this call, is that the system free the inactive memory used by the application.In that article recommends (in some situations) that this operation can be made from periodically; I think that this part is not accurate, but the explanation seems to me clear and correct.
Neftalí
@Neftalí - I didn't say that you've said this! I said that you recommend an article, which recommends doing that.
Alexander
SetProcessWorkingSetSize does not free memory. It tells the OS to page everything to disk. That's an immediate performance hit. Then, when you later use those swapped-out pages, you get another performance hit as they're paged back into RAM. A better strategy is to leave everything alone and let the OS page things out when it needs to.
Rob Kennedy
Nice Discussion going on and thx for the replies and information everyone. I did not really have any plans on using the function described above I was just curious how it was done. I try to write my code efficiently and as Rob said let the OS do it's thing.Cheers,Dave
dave
+1  A: 

See Barry Kelly's answer on this question.

Alexander