9

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

dave
  • 93
  • 3
  • "Memory" is very vague definition. What you see is working set decreasing. You may also find this article useful: http://blog.eurekalog.com/catching-memory-leaks/ – Alex Jul 26 '10 at 14:23
  • It's only vague after you work for Microsoft long enough. – Warren P Jul 28 '10 at 18:55

3 Answers3

5

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,

RBA
  • 12,337
  • 16
  • 79
  • 126
  • 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 Jul 26 '10 at 11:48
  • 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 Jul 28 '10 at 18:54
4

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

  • It would have helped me if the very clear explanation from Ian Marteens had been in English... – Marjan Venema Jul 26 '10 at 13:15
  • 2
    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. – Alex Jul 26 '10 at 14:20
  • 1
    @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. – Germán Estévez -Neftalí- Jul 26 '10 at 15:02
  • @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. – Germán Estévez -Neftalí- Jul 26 '10 at 15:07
  • @Neftalí - I didn't say that you've said this! I said that you recommend an article, which recommends doing that. – Alex Jul 26 '10 at 15:42
  • 5
    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 Jul 26 '10 at 15:59
  • 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 Jul 27 '10 at 12:06
1

See Barry Kelly's answer on this question.

Community
  • 1
  • 1
Alex
  • 5,477
  • 2
  • 36
  • 56