如何找出特定组件或类使用了多少内存? [英] How can I find out how much memory is used by a specific component or class?

查看:28
本文介绍了如何找出特定组件或类使用了多少内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

delphi 中是否可以检索单个组件使用的内存量?

Is it possible to retrieve the amount of memory that is used by a single component in delphi?

我正在从互联网上下载简单的字符串,我发现在下载过程结束时内存使用量高达 1 GB,但是当我查看包含我下载的所有内容的保存文件时,只有在千字节范围内,显然这些组件发生了一些事情,即使我销毁了它们.

I'm downloading simple strings from the internet and I see that the memory usage is up to a gigabyte at by the end of the downloading process, but when I look at the saved file which contains everything I downloaded the file is only in the kilobyte range, clearly there is something going on with the components, even though I destroy them.

例子:

procedure TForm1.OnCreate(Sender: TObject);
  var list: TStringList;
begin
  list:=TStringList.Create;
  list.LoadFromFile('10MB_of_Data.txt');
  list.destroy;
end;

我如何知道作为 TStringList 的列表"正在使用 10 MB 的内存空间?

How can I know that "list" as a TStringList is using 10 MB worth of space in memory?

谢谢.

推荐答案

我认为比较之前和之后的内存使用情况是解决这个问题的方法,因为没有简单的方法可以查看代码块分配了哪些内存事后...例如,对于上面的字符串列表,类本身只会占用少量内存,因为它由指向其他分配(即字符串数组)的指针组成,并且本身就是一个数组指向实际字符串的指针......这是一个相对简单的案例.

I think comparing the memory usage before and after is the way to go with this as there is no simple way of seeing what memory was allocated by a block of code after the fact... For example, with the string list above, the class itself will only take up a small amount of memory as it is made up of pointers to other allocations (i.e. the array of strings) and that itself is an array of pointers to to the actual strings... and this is a comparatively simple case.

无论如何,这可以使用 FastMM 来完成,其功能如下...

Anyway, this can be done with FastMM with a function like follows...

uses
  FastMM4;

function CheckAllocationBy(const AProc: TProc): NativeUInt;
var
  lOriginalAllocated: NativeUInt;
  lFinalAllocated: NativeUInt;
  lUsage: TMemoryManagerUsageSummary;
begin
  GetMemoryManagerUsageSummary(lUsage);
  lOriginalAllocated := lUsage.AllocatedBytes;
  try
    AProc;
  finally
    GetMemoryManagerUsageSummary(lUsage);
    lFinalAllocated := lUsage.AllocatedBytes;
  end;
  Result := lFinalAllocated - lOriginalAllocated;
end;

而且可以这样使用...

And can be used like so...

lAllocatedBytes := CheckAllocationBy(
  procedure
  begin
    list:=TStringList.Create;
    list.LoadFromFile('10MB_of_Data.txt');
    list.Free;
  end);

这会告诉你你的字符串列表留下了多少(有趣的是,我在第一次重复调用时得到 40 个字节,在调用前后查阅使用日志后得到 0 个字节是在第一次打电话).如果您想检查泄漏内存的分配位置,使用 FastMM 也很简单(尽管我同意上述观点,如果它是第 3 方,那应该不是您的问题).

This will tell you how much your string list left behind (which interestingly I get 40 bytes for on the first run of repeated calls and 0 after which after consulting the usage logs before and after the call is two encoding classes created on the first call). If you want to check where leaked memory was allocated, it's simple to use FastMM to do that also (although I agree with the above that if it's 3rd party, it shouldn't be your problem).

这篇关于如何找出特定组件或类使用了多少内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆