如何视窗知道我不是使用内存? [英] How does Windows know i'm not using the memory?

查看:125
本文介绍了如何视窗知道我不是使用内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,其中的内存,直到它被感动,而不是当它是的malloc -ed或<$ C $不是由Windows任务管理器为已分配注册了这个奇怪的效果C>新 -ed。在这两个调试时出现的效果优化发布的基础之上。

I noticed this weird effect where memory is not registered as allocated by the Windows Task Manager until it is touched instead of when it is malloc-ed or new-ed. The effect occurs in both the debug and optimized release builds.

下面是一个结构性的例子,虽然在我的code中的分配和利用发生在不同的线程,所以我不认为它的​​优化,虽然我不知道如何检查。

The following is a structural example, although in my code the allocation and utilization occurs on different threads so I don't think its the optimizer, although I am not sure how to check.

for (int i = 0 ;i < 1000;i++)
{
buffer[i]=malloc(buffersize);
}
_sleep(1000*60)
for (int i=0;i<1000;i++)
{
memset(buffer[i],0,buffersize);//Only shows up the in the resource manager here
}

我的问题是如何的Windows知道,我使用的内存?难道监视内存的使用量第一或者是一些编译时优化。

My question is how does Windows know I have used the memory? Is it monitoring memory for first usage or is it some compile time optimization.

我的好奇心被实时采集激发我写这要求我触碰内存的两倍。从而pressing的按钮(AQUIRE!)要求我在一次写的RAM 64千兆字节,而不是随时间,增加等待时间的相当真实量。如果我的malloc ,因为我去这增加了太多的等待时间。

My curiosity is motivated by a realtime acquisition I am writing that requires me to touch the memory twice -> once when allocating and once when actually filling it with data. Thus pressing a button ("aquire!") requires me to write 64 gigabytes of ram at once, as opposed to over time, adding a fairly real amount of latency. If I malloc as I go this adds too much latency.

- 编辑 -

我也被禁用Windows的页面文件...

I also disabled the Windows pagefile...

推荐答案

分配给您的进程的虚拟地址空间并不一定是物理内存的支持。一个例子是分页文件,其中内存的使用可能会转移到如果物理RAM已所剩无几。但是,并不一定是在所有如果你也从来没有写入到它分配的任何物理内存或磁盘空间。因此,作为优化,一些操作系统给你的虚拟地址空间而不为它实际上还没有分配空间。在某些情况下,这是非常有益的。在其他情况下,像你这样的,这是一个有点讨厌。

Virtual address space allocated to your process is not necessarily backed by physical RAM. One example is the paging file, where memory that's in use may be moved to if physical RAM is running low. But there doesn't have to be any physical RAM or disk space allocated at all if you've never even written to it. Hence, as an optimization, some operating systems give you virtual address space without actually allocating space for it yet. In some cases, this is very beneficial. In other cases, like yours, it's a bit annoying.

这是说,任务管理器是调查内存使用pretty简单的工具。它的越多,你能够观察到这是一个幸运的巧合,不能(可靠)只通过使用任务管理器观察到的很多问题。

That said, the task manager is a pretty simplistic tool for investigating memory usage. It's more of a lucky coincidence you were able to observe this, many problems can't (reliably) be observed using only the task manager.

至于如何避免懒分配不碰所有的记忆......我不知道任何方式。甚至没有虚拟提供解决的办法。
你可以只写每页一个字节,应该仍然会造成物理RAM分配降低成本一点。但请记住,即使你能避免编写的任何的时,Windows仍然不得不分配1600万页,包括页表项。这是必然要花费一些时间。您可以使用大页(使用虚拟),这让网页数百倍大,因此降低了上述1600万页,以一个比较合理的数字。

As for how to avoid lazy allocation without touching all memory... I don't know of any way. Not even VirtualAlloc offers a way around this. You can cut down the costs a bit by only writing one byte per page, that should still cause physical RAM allocation. But keep in mind, even if you could avoid writing anything, Windows would still have to allocate 16 million pages, including page table entries. That's bound to take some time. You can use large pages (using VirtualAlloc), which makes pages a few hundred times larger and hence reduces the aforementioned 16 million pages to a more reasonable figure.

这篇关于如何视窗知道我不是使用内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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