如何获取Delphi程序使用的内存
我知道如何使用 GlobalMemoryStatusEx 获取系统内存使用情况,但这告诉我整个操作系统正在使用什么。
我真的希望我的程序报告它单独分配和正在使用多少内存。
我的 Delphi 2009 程序中是否有任何方法可以调用 Windows 函数或某些 FastMM 函数来找出由我的程序单独分配的内存?
重新审视我的问题,我现在已将我接受的答案更改为 @apenwarr 的 GetMemoryManagerState 答案。 它产生的结果与我以前使用的 GetHeapStatus 函数(现已弃用)相同,而 GetProcessMemoryInfo.WorkingSetSize 给出了非常不同的结果。
I know how to get the System memory use using GlobalMemoryStatusEx, but that tells me the what the entire OS is using.
I really want my program to report how much memory it alone has allocated and is using.
Is there any way within my Delphi 2009 program to call either a Windows function or maybe some FastMM function to find out the memory that has been allocated by my program alone?
Revisiting my question, I have now changed my accepted answer to the GetMemoryManagerState answer by @apenwarr. It produced identical results to the GetHeapStatus function (now deprecated) that I used to use, whereas GetProcessMemoryInfo.WorkingSetSize gave a very different result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以从 Delphi 运行时获取有用的内存使用信息,而无需使用任何直接的 Win32 调用:
此方法的最佳之处在于它是严格跟踪的:当您分配内存时,它会增加,而当您释放内存时,它会减少立即相同的金额。 我在运行每个单元测试之前和之后使用它,这样我就可以知道哪个测试正在泄漏内存(例如)。
You can get useful memory usage information out of the Delphi runtime without using any direct Win32 calls:
The best thing about this method is that it's strictly tracked: when you allocate memory, it goes up, and when you deallocate memory, it goes down by the same amount right away. I use this before and after running each of my unit tests, so I can tell which test is leaking memory (for example).
来自旧博客我的帖子。
想知道您的程序使用了多少内存? 这个 Delphi 函数就可以解决这个问题。
不确定我从哪里获得了这方面的基础知识,但我为其添加了一些更好的错误处理并使其成为一个函数。 WorkSetSize 是当前使用的内存量。 您可以使用类似的代码来获取当前进程(或任何进程)的其他值。 您需要在使用语句中包含 psAPI。
PROCESS_MEMORY_COUNTERS 记录包括:
您可以在任务管理器或 Process Explorer 中找到所有这些值。
From an old blog post of mine.
Want to know how much memory your program is using? This Delphi function will do the trick.
Not sure where I got the basics of this, but I added some better error handling to it and made it a function. WorkingSetSize is the amount of memory currently used. You can use similar code to get other values for the current process (or any process). You will need to include psAPI in your uses statement.
The PROCESS_MEMORY_COUNTERS record includes:
You can find all of these values in Task Manager or Process Explorer.
当您从 SourceForge 下载完整的 FastMM4 捆绑包时,您可以查看有关如何将 FastMM 与UsageTrackerDemo 项目一起使用的示例,该项目包含在演示中。
You can look at an example on how to use FastMM with the UsageTrackerDemo project that comes included with the Demos when you download the complete FastMM4 bundle from SourceForge.
我编写了这个小函数来返回当前进程(应用程序)的内存使用情况:
I wrote this small function to return the current process (app) memory usage:
将 Gant C ++ 代码转换为 Delphi 中的控制台应用程序:
Conversion of Gant C ++ code, to console application in Delphi:
对于Win32 API方式,您需要GetProcessMemoryInfo函数。 以下是 MSDN 页面 中的示例,但代码是C++的。 我想你也可以将它转换为Delphi。 您正在寻找的可能称为“工作集大小”。
For Win32 API way, you need GetProcessMemoryInfo function. Here is an example from MSDN page but the code is in C++. I think you can convert it to Delphi as well. What you are looking is probably called "Working Set Size."