FastMM与Delphi10.4.2内存管理

FastMM与Delphi10.4.2内存管理

 

一、Delphi10.4.2内置FastMM4

      FastMM属付费Licence。

1、内置到系统运行时System.pas

1.1、常用的全局debug变量及方法


{ 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}

 

1.2、可用的条件判断参数:

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;//;当多核心系统线程数过低导致性能下降时不让线程休眠

2、System.pas依赖的SysInit.pas中还有一些与Hook相关的内容

      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}


      细节就不详述啦,自己去查看源码。

二、FastMM5新增功能

2.1、谢谢网友红鱼儿第一时间告知FastMM5发布

      https://www.cnblogs.com/kinglandsoft/p/12813157.html

2.2、原文

      https://github.com/pleriche/FastMM5

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专讲冷知识

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值