2

In search of memory leak, I have been using MemProof and be able to see live counts of resources being used, created and destroyed. After having run my program for over a day and half, I have noticed that everything else being constant or less, Virtual Memory (VM) is increasing in counts. It started out at 109 and now it is at 113 after 24 hours.

This is what MemProof says for each VM leak:

VirtualAlloc(address_location, 16384, 4096, 4); It is identified as Virtual Memory and its size is always 16384. API name is VirtualAlloc. The module is kernel32.dll.

Furthermore, memproof says, "virtualalloc reserves or commits a region of pages in the virtual address space of the calling process. Allocated pages must be freed with virtualFree when no longer needed."

VM leak is associated with a function in the file System.Pas.

The function is as follows:

function GetCmdShow: Integer;
var
  SI: TStartupInfo;
begin
  Result := 10;                  { SW_SHOWDEFAULT }
  GetStartupInfo(SI);
  if SI.dwFlags and 1 <> 0 then  { STARTF_USESHOWWINDOW }
    Result := SI.wShowWindow;   
end;  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

I have the less than signs pointing at the key word "end" is where Memproof takes me to when I click on virtual memory leak(s).

So, what does this mean?

ThN
  • 3,235
  • 3
  • 57
  • 115
  • possible duplicate of [Why TObject.Free is called before the first line of code of my application is executed?](http://stackoverflow.com/questions/5331783/why-tobject-free-is-called-before-the-first-line-of-code-of-my-application-is-exe) – David Heffernan Mar 17 '11 at 15:34
  • 1
    VirtualAlloc is the function that one calls to allocate memory from the system. If you stop calling VirtualAlloc you won't leak memory. You also won't be able to do dynamic allocation. Not very useful. I simply can't understand why you won't use the tool that tells you where your leaks are. – David Heffernan Mar 17 '11 at 15:45
  • David No where in my source files VirtualAlloc is called. The program is not directly calling VirtualAlloc. It is being called implicitly. By the way I would have brought this up in my previous question, although it is related to this question it is not the same. I am specifically asking about VirtualAlloc. – ThN Mar 17 '11 at 16:35
  • 1
    @user FastMM calls VirtualAlloc on your behalf when you ask FastMM for memory and it needs some more from the system. I think you should follow Mason's excellent advice. – David Heffernan Mar 17 '11 at 17:10

1 Answers1

5

Delphi's FastMM memory manager works on top of the Windows memory system. It allocates large blocks of memory from the OS with VirtualAlloc, and then divides that up into smaller chunks for your program to work with. If you free large amounts of memory, it will give some of it back to the OS. If you free small amounts of memory, though, it's likely to hold on to it because you're likely to need it again soon. This is part of what makes FastMM fast, and it's not a memory leak.

Any memory profiler that only watches VirtualAlloc and doesn't actually pay attention to what FastMM is doing is going to give you results that don't make sense. As David mentioned in the comment, if you want to track down real memory leaks, you need to use the FastMM tools. Download the full version of FastMM from SourceForge and read the documentation to find out how to enable FullDebugMode and leak reporting and logging, and you'll have a much easier time of it.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • @David and @Mason FastMM4 definitely takes some getting used to, because it is very cryptic and somewhat intimidating (if I may say so) in its user interface (log file). However, I spent sometime looking through the logfile after running my software and I am finding all kinds of leaks in my source file. Some of which are obvious but overlooked. Thanks for your help. – ThN Mar 17 '11 at 20:36
  • @user I agree that it spits out a lot of stuff. But that can often be essential for hard to trace leaks. And when you have a lot of leaks it's intimidating. I trust you'll now be able to accept an answer for yesterday's question too!!! – David Heffernan Mar 17 '11 at 20:38
  • See this answer for complete details howto enable FastMM4:http://stackoverflow.com/questions/1130454/how-to-get-a-stack-trace-from-fastmm/1130506#1130506 – johnny Mar 18 '11 at 08:58