RAM 性能计数器问题

发布于 2024-08-03 02:03:14 字数 1034 浏览 6 评论 0原文

我的代码:

var list = new LinkedList<int>();
var ramCounter = new PerformanceCounter("Memory", "Available MBytes");
while (true)
{
    for(int i = 0; i < 1000 * 1000; i++) list.AddLast(0);
    Console.WriteLine(ramCounter.NextValue());
}

问题:

  1. 文档似乎说我只能以管理员的身份使用PerformanceCounter,但我以标准用户的身份运行我的应用程序并且它有效。我可以依赖它吗?
  2. 当剩余大约 200 MB RAM 时,我总是遇到 OutOfMemoryException为什么?这不可能是由于碎片造成的,因为我一次分配了一个 int 。另外,这不可能是因为可寻址性问题,因为我已经远远超过 2GB(当我的 3GB 中的 2.8 被取出时会抛出异常)。测试应用程序输出的数字通过侧面运行的任务管理器窗口进行验证。
  3. 有一次,当测试应用程序运行且剩余空间大约为 400 MB 时,我遇到了 BSOD。关于什么可能导致这种情况的任何提示?我将检查 RAM 完整性,还有其他吗?我应该小心地在循环中调用PerformanceCounter.NextValue()吗?请注意,这是我第一次在这台电脑上遇到 BSOD。
  4. 在程序执行的某些时刻,我会遇到很大的延迟。例如,当我从 1 GB 可用 RAM 开始时,当我达到 700 MB 时,应用程序冻结 1 秒,然后在大约 400 MB 时,它冻结约 4 秒。这是为什么呢?因为操作系统需要交换磁盘缓存以释放内存,或者其他什么?

注意:我为什么要这样做?好吧,我希望我的内存密集型应用程序能够检测何时剩余 5 MB RAM,并提醒用户“内存不足,请关闭其他程序然后再回来,否则该程序将失败”。

My code:

var list = new LinkedList<int>();
var ramCounter = new PerformanceCounter("Memory", "Available MBytes");
while (true)
{
    for(int i = 0; i < 1000 * 1000; i++) list.AddLast(0);
    Console.WriteLine(ramCounter.NextValue());
}

Questions:

  1. The documentation seems to say I can use a PerformanceCounter only as an Administrator, but I ran my application as a Standard User and it worked. Can I rely on that?
  2. I consistently get OutOfMemoryException when about 200 MB RAM remain, why? It can't be due to fragmentation because I allocate an int at a time. Also, it can't be because of addressability issues, as I'm already way above 2GB (the exception gets thrown when 2.8 out of my 3GB are taken). The numbers output by the test app were verified with a Task Manager window running at the side.
  3. Once I got a BSOD when the test app was running and about 400 MB were remaining. Any hints as to what could possibly cause this? I'll run a check for RAM integrity, anything else? Should I be careful with calling PerformanceCounter.NextValue() in a loop, or something? Note, that's the first time I get a BSOD on this PC.
  4. At some points in the program execution, I get big delays. E.g. when I start at 1 GB free RAM, when I get to 700 MB the app freezes for 1 sec, then at about 400 MB it freezes for about 4 sec. Why is this? Because the OS needs to swap out disk caches to free up memory, or something?

Note: Why am I doing this? Well, I want my memory-intensive app to detect when 5 MB RAM remain, and alert the user with "Memory is low, please close other programs and come back, or this program will fail."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽月 2024-08-10 02:03:14

我无法解决您所有的问题,但这里是

2) 您使用的是 32 位还是 64 位 Windows?听起来您正在运行一个大型地址感知进程,可以访问 3 GB(即在 32 位 Windows 上)。无论如何,您必须记住,内存是在不同大小的卡盘中分配的,并且您的堆使用并不是唯一的来源。 CLR本身有无数的结构,每个线程都有一个堆栈等等。在任何情况下,您都不能真正期望能够使用正好 3 GB,

3) BSOD 是由于驱动程序或内核错误造成的。据我所知,您的应用程序不会导致 BSOD,因此这很可能是无关的。

4) 如果您使用大量内存,GC 将很难跟上。由于用户线程在 GC 的某些部分正在进行时被挂起,这将显着减慢应用程序的速度。

I can't address all your questions, but here goes

2) Are you on 32 or 64 bit Windows? It sounds like you're running a large address aware process with access to 3 GB (i.e. on 32 bit Windows). In any case you have to keep in mind that memory is allocated in chucks of various sizes and your heap usage is not the only source. The CLR itself has numerous structures, each thread has a stack and so on. In any case, you can't really expect to be able to use exactly 3 GB,

3) BSOD is due to driver or kernel errors. AFAIK your application cannot cause BSOD, so this is most likely unrelated.

4) If you use a lot of memory the GC will have a hard time keeping up. As user threads are suspended while certain parts of GC is in progress this will slow down your application significantly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文