FastMM属付费Licence。
{ Memory manager support }
procedure GetMemoryManager(var MemMgr: TMemoryManager); overload; deprecated;
procedure SetMemoryManager(const MemMgr: TMemoryManager); overload; deprecated;
procedure GetMemoryManager(var MemMgrEx: TMemoryManagerEx); overload;
procedure SetMemoryManager(const MemMgrEx: TMemoryManagerEx); overload;
function IsMemoryManagerSet: Boolean;
function SysGetMem(Size: NativeInt): Pointer;
function SysFreeMem(P: Pointer): Integer;
function SysReallocMem(P: Pointer; Size: NativeInt): Pointer;
function SysAllocMem(Size: NativeInt): Pointer;
function SysRegisterExpectedMemoryLeak(P: Pointer): Boolean;
function SysUnregisterExpectedMemoryLeak(P: Pointer): Boolean;
{ AllocMem allocates a block of the given size on the heap. Each byte in
the allocated buffer is set to zero. To dispose the buffer, use the
FreeMem standard procedure. }
function AllocMem(Size: NativeInt): Pointer;
var
AllocMemCount: Integer deprecated; {Unsupported}
AllocMemSize: Integer deprecated; {Unsupported}
{Set this variable to true to report memory leaks on shutdown. This setting
has no effect if this module is sharing a memory manager owned by another
module.}
ReportMemoryLeaksOnShutdown: Boolean;
{Set this variable to true to employ a "busy waiting" loop instead of putting
the thread to sleep if a thread contention occurs inside the memory manager.
This may improve performance on multi-CPU systems with a relatively low thread
count, but will hurt performance otherwise.}
NeverSleepOnMMThreadContention: Boolean;
{$IFDEF MSWINDOWS}
function GetHeapStatus: THeapStatus; platform; deprecated; {Unsupported}
{Returns information about the current state of the memory manager}
procedure GetMemoryManagerState(var AMemoryManagerState: TMemoryManagerState); platform;
{Gets the state of every 64K block in the 4GB address space}
procedure GetMemoryMap(var AMemoryMap: TMemoryMap); platform;
{Registers expected memory leaks. Returns true on success. The list of leaked
blocks is limited in size, so failure is possible if the list is full.}
function RegisterExpectedMemoryLeak(P: Pointer): Boolean; platform;
{Removes expected memory leaks. Returns true if the previously registered leak
was found and removed.}
function UnregisterExpectedMemoryLeak(P: Pointer): Boolean; platform;
{Set the minimum block alignment. In the current implementation blocks >=160
bytes will always be at least 16 byte aligned, even if only 8-byte alignment
(the default) is required.}
function GetMinimumBlockAlignment: TMinimumBlockAlignment; platform;
procedure SetMinimumBlockAlignment(AMinimumBlockAlignment: TMinimumBlockAlignment); platform;
{Searches the current process for a shared memory manager. If no memory has
been allocated using this memory manager it will switch to using the shared
memory manager instead. Returns true if another memory manager was found and
this module is now sharing it.}
function AttemptToUseSharedMemoryManager: Boolean; platform;
{Makes this memory manager available for sharing to other modules in the
current process. Only one memory manager may be shared per process, so this
function may fail.}
function ShareMemoryManager: Boolean; platform;
{$ENDIF}
var
HeapAllocFlags: Word platform = 2; { Heap allocation flags, gmem_Moveable }
DebugHook: Byte platform = 0; { 1 to notify debugger of non-Delphi exceptions
>1 to notify debugger of exception unwinding }
JITEnable: Byte platform = 0; { 1 to call UnhandledExceptionFilter if the exception
is not a Pascal exception.
>1 to call UnhandledExceptionFilter for all exceptions }
NoErrMsg: Boolean platform = False; { True causes the base RTL to not display the message box
when a run-time error occurs }
{$IF defined(LINUX) or defined(MACOS) or defined(ANDROID)}
{ CoreDumpEnabled = True will cause unhandled
exceptions and runtime errors to raise a
SIGABRT signal, which will cause the OS to
coredump the process address space. This can
be useful for postmortem debugging. }
CoreDumpEnabled: Boolean platform = False;
implementation
uses
SysInit;
常见用法举例:
//.......项目初始化前:
//下面根据调试或运行使用的场景选用:
//ReportMemoryLeaksOnShutdown := True; // 只要有内存泄漏就会报
ReportMemoryLeaksOnShutdown := DebugHook<>0;
//是Delphi的异常才报,有点绕,什么意思呢:
//1、当Debug调试时:不是Delphi的错误会抛出;
//:(不是指的在Build Config=Debug时点Run without Dubugging (Shift+Ctrl+F9),而是指此时点Run(F9)即真正做调试 );
//2、Release时(Build Config=Release):不抛出只响铃
//ReportMemoryLeaksOnShutdown := DebugHook = 1;//:(响铃)如果不是Delphi的异常(通知调试器这是非Delphi的异常)
//ReportMemoryLeaksOnShutdown := DebugHook > 1;//:通知调试器异常解除
//.......在你需要调试的代码位置之后类似这样写:
//..............我这个写法是运行时屏蔽抛出那些我认为无关紧要的异常只响铃:
if DebugHook <> 0 then//:如果你在Build Config=Debug时点了Run(F9)即在做调试
begin
if DebugHook =1 then Raise Exception.Create('操作系统访问地址冲突!')
//:(响铃)即在这里Raise抛出正确的非Delphi异常信息Exception.Create('WWindows组件地址访问错误comctl32.dll')
else if DebugHook >1 then ; //:会自动通知调试器异常解除:啥都不用写
end else Raise Exception.Create(string.Empty); //:如果你未做调试,抛出空的提示(响铃)
NeverSleepOnMMThreadContention := True;//;当多核心系统线程数过低导致性能下降时不让线程休眠
SysInit.pas的实现库:
implementation
{$IFDEF MSWINDOWS}
const
kernel = 'kernel32.dll';
//........................
var
tlsBuffer: Pointer; // RTM32 DOS support
{$ENDIF}
{$IFNDEF POSIX} // Used for unsigned operations with -1. POSIX version in block below.
const
NEG_ONE = LongWord(-1);
{$ENDIF}
{$IFDEF POSIX}
{$I PosixAPIs.inc}
//........................
{$ENDIF POSIX}
细节就不详述啦,自己去查看源码。
https://www.cnblogs.com/kinglandsoft/p/12813157.html
https://github.com/pleriche/FastMM5