Below, for my link archive, some searches and relevant posts on FastMM related method calls to track or report memory usage.
Searches:
- LogMemoryManagerStateToFile
- FastGetHeapStatus {Returns summarised information about the state of the memory manager. (For
backward compatibility.)} - GetMemoryManagerState (InternalBlockSize, UseableBlockSize, AllocatedBlockCount, ReservedAddressSpace) {Returns statistics about the current state of the memory manager}GetMemoryManagerUsageSummary {Returns a summary of the information returned by GetMemoryManagerState}
- GetMemoryMap {Non-POSIX only; Gets the state of every 64K block in the 4GB address space}
- ScanMemoryPoolForCorruptions; {Scans the memory pool for any corruptions. If a corruption is encountered an “Out of Memory” exception is raised.}
- It is very costly in CPU usage, but helps finding heap corruption quickly.
- function GetCurrentAllocationGroup: Cardinal;
- {Returns the current “allocation group”. Whenever a GetMem request is serviced
in FullDebugMode, the current “allocation group” is stored in the block header.
This may help with debugging. Note that if a block is subsequently reallocated
that it keeps its original “allocation group” and “allocation number” (all
allocations are also numbered sequentially).}
- {Returns the current “allocation group”. Whenever a GetMem request is serviced
- procedure PushAllocationGroup(ANewCurrentAllocationGroup: Cardinal);
procedure PopAllocationGroup;- {Allocation groups work in a stack like fashion. Group numbers are pushed onto
and popped off the stack. Note that the stack size is limited, so every push
should have a matching pop.}
- {Allocation groups work in a stack like fashion. Group numbers are pushed onto
- LogAllocatedBlocksToFile
- {Logs detail about currently allocated memory blocks for the specified range of
allocation groups. if ALastAllocationGroupToLog is less than
AFirstAllocationGroupToLog or it is zero, then all allocation groups are
logged. This routine also checks the memory pool for consistency at the same
time, raising an “Out of Memory” error if the check fails.}
- {Logs detail about currently allocated memory blocks for the specified range of
- SetMMLogFileName
- {Specify the full path and name for the filename to be used for logging memory
errors, etc. If ALogFileName is nil or points to an empty string it will
revert to the default log file name.}
- {Specify the full path and name for the filename to be used for logging memory
Posts (note that not all of them get their calculations right):
- [WayBack] How to get the Memory Used by a Delphi Program – Stack Overflow
- [WayBack] delphi – FastMM: Total Allocated Memory – Stack Overflow
- [WayBack] delphi – Does calling FastMM4 LogAllocatedBlocksToFile() periodically use up memory space? – Stack Overflow:
I have tracked this down to be a version mismatch of the support library
FastMM_FullDebugMode.dll.An older version of the library works with the newer version compiled into the executable. There seems to be no check that versions do match. However, modules don’t really work together at run-time.
- [WayBack] delphi – How to solve memory segmentation and force FastMM to release memory to OS? – Stack Overflow
- [WayBack] TURBU Tech » Blog Archive » Wanted: live leak detection for FastMM
- [WayBack] Strategy or tools to find “non-leak” memory usage problems in Delphi? – Stack Overflow:
- use non-FastMM tools: AQTime, MemProof, SleuthQA
- use FastMM methods GetMemoryManagerSTate, GetMemoryManagerUsageSummary, LogMemoryStateToFile
FastMM4991 introduced a new method,
LogMemoryManagerStateToFile:Added the LogMemoryManagerStateToFile call. This call logs a summary of the memory manager state to file: The total allocated memory, overhead, efficiency, and a breakdown of allocated memory by class and string type. This call may be useful to catch objects that do not necessarily leak, but do linger longer than they should.
- [WayBack] delphi – How can I find out how much memory is used by a specific component or class? – Stack Overflow
- [WayBack] What does GetMemoryManagerState, ReservedAddressSpace do in Delphi? – Stack Overflow
These help you track leaks that do not appear as leaks during shutdown: memory allocations that will be released at the end of your application, but are mostly unused while your application is still alive.
A few things to take away from these:
- “Out of Memory” (or exception
EOutOfMemor) could mean that the memory manager structures are hosed, but memory is still available. - You can specify the FastMM log file used (for instance to include a PID or run-time ID in them so each run gets a separate filename)
- When carefully setting up allocation groups, you are able to zoom in at allocations
A gist with a MemoryManagerUnit showing a few of these calls is below.
An example of its usage is this:
procedure TMyTestClass.TestMethod(); begin TLogMemoryStatesHelper.DumpMemoryStatesBeforeAndAfter('TestMethod', TLogging.LogDir, TFileLogger.GetLogFileName, procedure (const AFormat: string; const Args: array of const) begin TLogging.LogEvent(ltInfoHigh, aFormat, Args); end, procedure() begin try // Given BuildSomeTestScenario(); // When InitializeTestScenario(); // Then CheckEquals(0, TestScenarioSummary()); finally // Cleanup CleanUpTestScenario(); end; end ); end;
–jeroen






