如何获取Delphi程序使用的内存 [英] How to get the Memory Used by a Delphi Program
问题描述
我知道如何使用GlobalMemoryStatusEx来使系统内存使用,但这告诉我整个操作系统正在使用什么。
我真的希望我的程序能够报告很多内存,它一直分配和正在使用。
我的Delphi 2009程序有什么办法可以调用Windows函数还是一些FastMM函数来查找我的程序分配的内存? / p>
重新审视我的问题,现在我已经将我接受的答案改为了@apenwarr的GetMemoryManagerState答案。它产生了与以前使用的GetHeapStatus函数(现在已被弃用)相同的结果,而GetProcessMemoryInfo.WorkingSetSize给出了非常不同的结果。
您可以从Delphi运行时获取有用的内存使用信息,而无需使用任何直接的Win32调用:
function MemoryUsed:cardinal;
var
st:TMemoryManagerState;
sb:TSmallBlockTypeState;
begin
GetMemoryManagerState(st);
result:= st.TotalAllocatedMediumBlockSize + st.TotalAllocatedLargeBlockSize;
for sb在st.SmallBlockTypeStates中开始
result:= result + sb.UseableBlockSize * sb.AllocatedBlockCount;
结束
结束
这个方法的最好的方法是严格跟踪:分配内存时,它会上升,当你释放内存时,它立即下降相同的数量。我在运行每个单元测试之前和之后使用它,所以我可以告诉哪个测试是泄漏内存(例如)。
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.
You can get useful memory usage information out of the Delphi runtime without using any direct Win32 calls:
function MemoryUsed: cardinal;
var
st: TMemoryManagerState;
sb: TSmallBlockTypeState;
begin
GetMemoryManagerState(st);
result := st.TotalAllocatedMediumBlockSize + st.TotalAllocatedLargeBlockSize;
for sb in st.SmallBlockTypeStates do begin
result := result + sb.UseableBlockSize * sb.AllocatedBlockCount;
end;
end;
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程序使用的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!