windows 有没有办法限制进程的 CPU/内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/593760/
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
Is there any way of throttling CPU/Memory of a process?
提问by Matthew Farwell
Problem: I have a developers machine (read: fast, lots of memory), but the user has a users machine (read: slow, not very much memory).
问题:我有一台开发者机器(读取:快速,大量内存),但用户有一台用户机器(读取:慢,内存不大)。
I can simulate a slow network using Fiddler (http://www.fiddler2.com/fiddler2/) I can look at how CPU is used over time for a process using Process Explorer (http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx).
我可以使用 Fiddler ( http://www.fiddler2.com/fiddler2/)模拟慢速网络我可以使用 Process Explorer ( http://technet.microsoft.com/en -us/sysinternals/bb896653.aspx)。
Is there any way I can restrict the amount of CPU a process can have, or the amount of memory a process can have in order to simulate a users machine more effectively? (In order to isolate performance problems for instance)
有什么方法可以限制一个进程可以拥有的 CPU 数量,或者一个进程可以拥有的内存量,以便更有效地模拟用户机器?(例如为了隔离性能问题)
I suppose I could use a VM, but I'm looking for something a bit lighter.
我想我可以使用虚拟机,但我正在寻找更轻松的东西。
I'm using Windows XP, but a solution for any Windows machine would be welcome. Thanks.
我使用的是 Windows XP,但欢迎为任何 Windows 机器提供解决方案。谢谢。
采纳答案by vladr
The platform SDK used to come with stress tools for doing just this back in the good old days (STRESS.EXE
, CPUSTRESS.EXE
in the SDK), but they might still be there (check your platform SDK and/or Visual Studio installation for these two files -- unfortunately I have niether the PSDK nor VS installed on the machine I'm typing from.)
在过去的好日子里STRESS.EXE
,平台 SDK 曾经带有用于执行此操作的压力工具(CPUSTRESS.EXE
在 SDK 中),但它们可能仍然存在(检查您的平台 SDK 和/或 Visual Studio 安装是否有这两个文件——不幸的是我在打字的机器上既没有安装 PSDK 也没有安装 VS。)
Other tools:
其他工具:
- memory: performance & reliability (e.g. handling failed memory allocation): can use EatMem
- CPU: performance & reliability (e.g. race conditions): can use CPU Burn, Prime95, etc
- handles(GDI, User): reliability (e.g. handling failed GDI resource allocation): ??? may have to write your own, but running out of GDI handles (buggy GTK apps would usually eat them all away until all other apps on the system would start falling dead like flies) is a real test for any Windows app
- disk: performance & reliability (e.g. handling disk full): DiskFiller, etc.
- memory:性能和可靠性(例如处理失败的内存分配):可以使用EatMem
- CPU:性能和可靠性(例如竞争条件):可以使用CPU Burn、Prime95等
- 句柄(GDI,用户):可靠性(例如,处理失败的 GDI 资源分配):???可能必须自己编写,但是用完 GDI 句柄(有缺陷的 GTK 应用程序通常会将它们全部吃掉,直到系统上的所有其他应用程序开始像苍蝇一样死掉)对任何 Windows 应用程序都是一个真正的考验
- disk:性能和可靠性(例如处理磁盘已满):DiskFiller等。
回答by George V. Reilly
AppVerifierhas a low-resource simulation feature.
AppVerifier具有低资源模拟功能。
You could also try setting the priority of your process to be very low.
您也可以尝试将进程的优先级设置得非常低。
回答by Stephen Denne
回答by coobird
I found a related question:
我发现了一个相关的问题:
Set Windows process (or user) memory limit
The accepted answer for the question has a link to the Windows API's SetProcessWorkingSetSize
, so it's not exactly a tool that can limit the amount of memory that a process can use.
该问题的公认答案有一个指向 Windows API 的链接SetProcessWorkingSetSize
,因此它并不是一个可以限制进程可以使用的内存量的工具。
In terms of changing the amount of CPU resources a process can use, if you don't mind the granularity of per-core limiting of resources, Task Manager can change the processor affinity of a process.
在更改进程可以使用的 CPU 资源量方面,如果您不介意每核资源限制的粒度,任务管理器可以更改进程的处理器亲和性。
In Task Manager, right-click a process and select "Set Affinity...", then select the processor cores that the process can be assigned to.
在任务管理器中,右键单击一个进程并选择“设置关联...”,然后选择该进程可以分配到的处理器内核。
If the development machine has many cores but the user machine only has one, then, rather than allowing the process to run on all the available cores, set the process' processor affinity to only one core.
如果开发机器有许多内核,而用户机器只有一个内核,那么,不要让进程在所有可用内核上运行,而是将进程的处理器关联设置为只有一个内核。
回答by coobird
It has nothing to do with SetProcessWorkingSetSize
它与 SetProcessWorkingSetSize 无关
Just use internal Win32 kernel apis to restrict CPU Usage
只需使用内部 Win32 内核 apis 来限制 CPU 使用率