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

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

问题描述

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

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

我正在从Internet下载简单的字符串,我看到了到下载过程结束时,内存使用量已达到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?

谢谢。

推荐答案

我想比较一下之前的内存使用情况之后就是这样做的方法,因为在事后没有简单的方法查看代码块分配的内存...例如,使用上面的字符串列表,该类本身将只占用一小部分$ b的内存量,它由指向其他分配的指针(即字符串数组)组成,并且本身就是指向实际字符串的指针数组……这是一个相对简单的情况。

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,在查询之前和之后的使用情况日志之后为0,这是在第一次调用上创建的两个编码类。如果您想检查内存泄漏的分配位置,那么也可以使用FastMM轻松做到这一点(尽管我同意上面的观点,如果是第三方,那不应该是您的问题)。

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天全站免登陆