设置 Windows 进程(或用户)内存限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/192876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:32:26  来源:igfitidea点击:

Set Windows process (or user) memory limit

windowsmemory

提问by Mike Minicki

Is there any way to set a system wide memory limit a process can use in Windows XP? I have a couple of unstable apps which do work ok for most of the time but can hit a bug which results in eating whole memory in a matter of seconds (or at least I suppose that's it). This results in a hard reset as Windows becomes totally unresponsive and I lose my work.

有没有办法设置一个进程可以在 Windows XP 中使用的系统范围内存限制?我有几个不稳定的应用程序,它们在大部分时间都可以正常工作,但可能会遇到一个错误,导致在几秒钟内吃掉整个内存(或者至少我想就是这样)。这会导致硬重置,因为 Windows 变得完全没有响应并且我丢失了我的工作。

I would like to be able to do something like the /etc/limits on Linux - setting M90, for instance (to set 90% max memory for a single user to allocate). So the system gets the remaining 10% no matter what.

我希望能够在 Linux 上执行类似于 /etc/limits 的操作 - 例如设置 M90(为单个用户设置 90% 的最大内存分配)。因此,无论如何,系统都会获得剩余的 10%。

采纳答案by Adam Mitz

Use Windows Job Objects. Jobs are like process groups and can limit memory usage and process priority.

使用 Windows作业对象。作业就像进程组,可以限制内存使用和进程优先级。

回答by Jesse Vogt

Use the Application Verifier (AppVerifier)tool from Microsoft.

使用Microsoft的应用程序验证程序 (AppVerifier)工具。

In my case I need to simulate memory no longer being available so I did the following in the tool:

就我而言,我需要模拟不再可用的内存,因此我在工具中执行了以下操作:

  1. Added my application
  2. Unchecked Basic
  3. Checked Low Resource Simulation
    • Changed TimeOut to 120000 - my application will run normally for 2 minutes before anything goes into effect.
    • Changed HeapAlloc to 100 - 100% chance of heap allocation error
    • Set Stacks to true - the stack will not be able to grow any larger
  4. Save
  5. Start my application
  1. 添加了我的应用程序
  2. 未选中的基本
  3. 检查低资源模拟
    • 将 TimeOut 更改为 120000 - 我的应用程序将在任何生效之前正常运行 2 分钟。
    • 将 HeapAlloc 更改为 100 - 100% 的堆分配错误几率
    • 将 Stacks 设置为 true - 堆栈将无法变大
  4. 节省
  5. 开始我的申请

After 2 minutes my program could no longer allocate new memory and I was able to see how everything was handled.

2 分钟后,我的程序无法再分配新的内存,我能够看到一切是如何处理的。

回答by Eric

Depending on your applications, it might be easier to limit the memory the language interpreter uses. For example with Java you can set the amount of RAM the JVM will be allocated.

根据您的应用程序,限制语言解释器使用的内存可能更容易。例如,对于 Java,您可以设置 JVM 将分配的 RAM 量。

Otherwise it is possible to set it once for each process with the windows API

否则可以使用 windows API 为每个进程设置一次

SetProcessWorkingSetSize Function

SetProcessWorkingSetSize 函数

回答by Nick

No way to do this that I know of, although I'm very curious to read if anyone has a good answer. I have been thinking about adding something like this to one of the apps my company builds, but have found no good way to do it.

我知道没有办法做到这一点,尽管我很想知道是否有人有好的答案。我一直在考虑在我公司构建的应用程序之一中添加类似的东西,但没有找到好的方法。

The one thing I can think of (although not directly on point) is that I believe you can limit the total memory usage for a COM+ application in Windows. It would require the app to be written to run in COM+, of course, but it's the closest way I know of.

我能想到的一件事(虽然不是直接点)是我相信您可以限制 Windows 中 COM+ 应用程序的总内存使用量。当然,这需要编写应用程序以在 COM+ 中运行,但这是我所知道的最接近的方式。

The working set stuff is good (Job Objects also control working sets), but that's not total memory usage, only real memory usage (paged in) at any one time. It may work for what you want, but afaik it doesn't limit total allocated memory.

工作集的东西很好(作业对象也控制工作集),但这不是总内存使用量,只是任何时候的实际内存使用量(调入)。它可能适用于您想要的,但它不限制总分配的内存。