My program never releases the memory back. Why?

前端 未结 5 493
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 14:41

I have a MDI program. When It starts it takes 2-3MB of RAM. Then, in this program I create about 260 MDI child windows (each has a TStringGrid, a bitmap and some other contr

相关标签:
5条回答
  • 2020-11-30 15:00

    Task Manager is not the right tool to detect memory leaks. Delphi allocates large blocks of memory and keeps them later for future use, so certain increase in allocated memory is expected even after you release all resources. Any other results (and more detailed answers) can be obtained only by using specialize memory analysis tools. AQTime is the first that comes to mind, or if you can find old but useful MemProof, it would help you a lot (MemProof is free and for memory analysis it's more handy than AQTime).

    0 讨论(0)
  • 2020-11-30 15:09

    It is very well possible that FastMM does not show memory leaks upon application termination (for instance because all objects are TComponents that are owned, and the ownser frees them).
    But in the mean time, while running those components can still be around, and not freed soon enough.

    Did you use the FastMM unit that shows a form with the current memory usage?

    < Edit >
    This is the FastMMUsageTracker.pas in the directory ...\FastMM\Demos\Usage Tracker.
    Use that unit, then call the ShowFastMMUsageTracker function in it. You can refresh that form every once in a while to see how your memory consumption grows. I have put a FastMMUsageTrackerProject sample on-line, including an update of FastMM4 that makes it easier to check and debug memory leaks:

    • the form in the FastMMUsageTracker unit is now resizable, and the controls in it anchor in the right way
    • there is a new FastMmBootstrapUnit unit making debugging specific memory leaks easier

    Something I had at hand last week, was a 3rd party DLL, which was not written in Delphi.
    The DLL had a memory leak using Windows GlobalAlloc calls, which are not tracked by FastMM.

    NB: I'm about to post an update to FastMM on

    --jeroen

    0 讨论(0)
  • 2020-11-30 15:11

    The main limitation of FastMM's memory leak tracing is that it can only run when you shut down the program. It could be that you're still holding references to objects or other data that gets cleaned up when you shut down the program, but stays around until then.

    For example, when you close the MDI child windows, do you call Free or Release on them, or just make them disappear? If they're hidden but not freed, they'll still be in memory.

    0 讨论(0)
  • 2020-11-30 15:17

    If you close an MDI form it is not freed automatically. Use Action = caFree (google for that) to make sure the form is also freed.

    0 讨论(0)
  • 2020-11-30 15:18

    Answer:

    I just removed FastMM from my project and the program returned to few MB after freeing all those child windows. Many may argue that this is not a misbehavior and that FastMM is doing this in order to do some kind of kinky memory optimizations. They may be true. However, it may be good for MY application but it may not be good of other running applications.

    So, at least we know who causes this. I worried to a whole day that may program is leaking RAM like an old bucket. I am relieved now.

    UPDATE:

    To confirm that this behavior is generated by FastMM (as suggested by Barry Kelly) I created a second program that allocated A LOT of RAM. As soon as Windows ran out of RAM, my program memory utilization returned to its original value.
    (Note: I am not saying there is a bug in FastMM!)

    My program is not leaking. Problem solved.

    0 讨论(0)
提交回复
热议问题