非托管内存未显示在任务管理器中 [英] Unmanaged memory not showing up in task manager

查看:30
本文介绍了非托管内存未显示在任务管理器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的测试(实际上用在更广泛的上下文中)

I wrote the following test (actually used in a wider context)

IntPtr x = Marshal.AllocHGlobal(100000000);

Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);

Marshal.FreeHGlobal(x);

Console.ReadKey(true);

为什么在第一次按键之前,任务管理器没有显示任何已分配 100 MB 的迹象?如果这是设计使然,我还能如何测试非托管堆内存的消耗?

Why doesn't the task manager show any sign of the allocated 100 megabytes before the first key press? If this is by design, how else can I test the consumption of unmanaged heap memory?

推荐答案

您对操作系统的工作方式有了一些了解.此行为并非特定于 Marshal.AllocHGlobal(),请尝试以下代码,例如:

You are getting some insight in how your operating system works. This behavior is not specific to Marshal.AllocHGlobal(), try this code for example:

    static void Main(string[] args) {
        var arr = new byte[100000000];
        Console.ReadKey(true);
    }

任务管理器默认会显示您使用了多少内存.但是您的操作系统与许多其他操作系统一样,是一个按需分页的虚拟内存操作系统.你还没有要求什么.你所做的就是分配虚拟内存.只是地址空间,它在这个测试中保持虚拟,只是处理器的数字.每 4096 个字节一个.在您实际访问数组之前,不会发生需求.添加:

Task Manager by default shows you how much RAM you use. But your OS, like many others, is a demand-paged virtual memory operating system. You haven't demanded anything yet. All you did is allocate virtual memory. Just address space, it stays virtual in this test, just numbers to the processor. One for each 4096 bytes. The demand doesn't happen until you actually access the array. Add:

        for (int ix = 0; ix < arr.Length; ix += 4096) {
            byte dummy = arr[ix];
        }

Bam,现在你看到它放大了.不一定要 100 兆字节,但大多数机器现在都有足够的 RAM,不需要将任何页面换回分页文件以提供足够的存储空间.

Bam, now you see it zoom up. Not necessarily to 100 megabytes, but most machines now have enough RAM to not need any pages to be swapped back out to the paging file to provide enough storage.

另外一个很好的提醒,为什么任务管理器不是一个很好的内存分析器.您的程序消耗多少 RAM 是无关紧要的.如果你使用太多,那么你会注意到它足够好,你的程序会变慢很多.

Otherwise a good reminder why Task Manager is not a very good memory profiler. How much RAM your program consumes is pretty irrelevant. If you use too much then you'll notice it well enough, your program slows down a lot.

任务管理器可以显示原始代码的副作用,您必须添加提交大小"列.在早期的 Windows 版本上命名不同,内存模糊,我认为是 VM Size.提交大小衡量在分页文件中保留了多少空间.地址空间的备份存储,保留以便在操作系统需要取消映射页面以在其他地方提供 RAM 时存储 RAM 内容.Windows 不允许过度使用内存,这是在 64 位程序中无法足够快地增长分页文件时出现 OOM 的方式.

Task Manager can show you the side-effect of the original code, you have to add the "Commit size" column. Named differently on earlier Windows versions, fuzzy memory, I think it was VM Size. Commit size measures how much space is reserved in the paging file. Backup storage for the address space, reserved so the RAM content can be stored when the OS needs to unmap pages to provide RAM elsewhere. Windows does not permit over-committing memory, it is the way you get OOM in a 64-bit program when it can't grow the paging file quickly enough.

这篇关于非托管内存未显示在任务管理器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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