设置最大内存使用量 C# [英] Set Maximum Memory Usage C#

查看:88
本文介绍了设置最大内存使用量 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序必须使用一个不是我编写的库,而且我无权更改它.基本上是内存泄漏,所以运行的时间越长,泄漏的越多;它基本上只是写入我无权删除的陈旧页面文件.内存泄漏实际上并不会导致程序崩溃,它只是简单地吃光了所有内存.当它使用系统的所有内存时,操作系统将开始删除旧的页面文件,一切正常.

I have an application that must use a library that I didn't write and I don't have the power to change it. Basically there is a memory leak, so the long it runs, the more it leaks; it basically just writes stale pagefiles that I don't have the power to delete. The memory leak doesn't actually cause the program to crash, it just simply eats up all the memory. When it is using all the system's memeory, the OS will just start deleting the old page files and everything runs fine.

如果我在 2gb 系统上运行我的应用程序,它将使用 2gb 并继续运行;16GB 系统也一样.

If I run my app on a 2gb system, it will use 2 gb and continue to run; same for a 16gb system.

有没有办法设置应用程序可以使用的内存量?

Is there a way to set the amount of memory an application can use?

推荐答案

这是 Windows 中每个进程的限制.每个进程在 32 位机器中获得大约 2GB 的虚拟地址空间(这是堆使用的).恐怕您对此无能为力.

That is a per-process limitation in Windows. Each process gets ~2GB virtual address space (that is what Heap makes use of) in a 32-bit machine. And I am afraid there is not much you can do about it.

粗略地说,CLR 能够在内存中容纳约 1.6GB 的对象.这对于大多数应用程序来说应该足够了.如果没有,那么您需要处理您的应用程序.

Roughly, CLR is able hold upto ~1.6GB of objects in memory. That should be enough for most of the applications. If not, then you need to work on your application.

就我而言,我遇到了类似的问题,然后使用 SqlDataReader 获取指定块大小的对象、处理它、计算它、从内存中清除它,然后获取另一个块.

In my case, I faced a similar problem and then used SqlDataReader to fetch objects in a specified chunk size, process it, compute it, clean it from memory, and then fetch another chunk.

MSDN 上还有一篇详细的文章 - 调查内存问题

There is also a detailed article on MSDN - Investigating Memory Issues

希望这会有所帮助.

这篇关于设置最大内存使用量 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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