小编Tor*_*edo的帖子

在 Delphi 应用程序中在 EOutOfMemory 之前获取可用内存(地址空间)

我有一个 32 位 Delphi 应用程序,在 /LARGEADDRESSAWARE 标志上运行。这允许在 64 位系统上分配最多 4GB。

我使用线程(在池中)来处理文件,其中每个任务在内存中加载一个文件。当多个线程正在运行时(正在加载多个文件),在某些时候 EOutOfMemory 会击中我。

获取可用地址空间的正确方法是什么,以便我可以在处理下一个文件之前检查我是否有足够的内存?

就像是:

如果 TotalMemoryUsed {from GetMemoryManagerState} + FileSize < "AvailableUpToMaxAddressSpace" 然后 NoOutOfMemory

我试过使用

TMemoryStatusEx.ullAvailVirtual for AvailableUpToMaxAddressSpace

但结果不正确(有时为 0,有时> 比我实际拥有的值)。

memory delphi memory-management

5
推荐指数
1
解决办法
861
查看次数

将Delphi构造函数称为普通方法 - 任何隐藏的副作用?

我很困惑:为什么显式调用Delphi构造函数/作为普通方法不会创建新实例/为什么没有内存泄漏?

这是一些示例代码:

TMyHelperClass = class(TObject)
private
  fSomeHelperInt: integer;
public
  property SomeHelperInt : integer read fSomeHelperInt write fSomeHelperInt;
  constructor Create (const initSomeHelperInt : integer);
end;

TMyClass = class(TObject)
private
  fHelper : TMyHelperClass;
public
  constructor Create(const initSomeInt: integer);
  destructor Destroy; override;
  property Helper : TMyHelperClass read fHelper;
end;
Run Code Online (Sandbox Code Playgroud)

执行:

constructor TMyHelperClass.Create(const initSomeHelperInt: integer);
begin
  fSomeHelperInt := initSomeHelperInt;
end;

constructor TMyClass.Create(const initSomeInt: integer);
begin
  fHelper := TMyHelperClass.Create(initSomeInt);
end;

destructor TMyClass.Destroy;
begin
  fHelper.Free;
  inherited;
end;
Run Code Online (Sandbox Code Playgroud)

用法:

var
  my : TMyClass;
begin
  my := TMyClass.Create(2016); …
Run Code Online (Sandbox Code Playgroud)

delphi constructor

2
推荐指数
1
解决办法
286
查看次数

标签 统计

delphi ×2

constructor ×1

memory ×1

memory-management ×1