Linux中如何确定进程内存限制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4651234/
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
How to determine the process memory limit in Linux?
提问by rk2010
I have been scouring the internet to find out how much memory a java process can take on a linux (red-hat) machine. ( I am not talking about heap; rather, the entire amount of memory taken up by the java Process)
我一直在网上搜索,以了解一个 java 进程可以在 linux(red-hat)机器上占用多少内存。(我不是在谈论堆;而是 Java Process占用的全部内存量)
I don't have permission to execute anything on that machine. So I can't simply execute a program that consumes memory until Out-Of-Memory condition.
我无权在该机器上执行任何操作。所以我不能简单地执行一个消耗内存的程序,直到出现内存不足的情况。
However, I do have permission to check config files, etc. ( for example: I tried to execute cat /proc/meminfo, but I can't understand it; it appears that none of its results stand for the parameter I want to know about).
但是,我确实有权限检查配置文件等(例如:我尝试执行 cat /proc/meminfo,但我无法理解它;它的结果似乎都不代表我想知道的参数关于)。
I have tried out a java program on a separate red hat machine - on which I do have permission to execute programs - and I was able to see java program grow up to around 3GB.
我已经在一台单独的红帽机器上尝试了一个 java 程序——我确实有权在它上面执行程序——并且我能够看到 java 程序增长到大约 3GB。
Is there some way I can find out how much memory a process can get ?
有什么方法可以找出一个进程可以获得多少内存?
采纳答案by bmargulies
ulimit is your friend. Java processes are no different than any others. But if you can't even run ulimit -a, it's hard to answer your question.
ulimit 是你的朋友。Java 进程与其他进程没有什么不同。但是,如果您甚至无法运行 ulimit -a,就很难回答您的问题。
回答by kvz
Here's a useful read: Limiting time and memory consumption of a program in Linux, which lead to the timeouttool, which lets you cage a process (and it's forks) by time or memory consumption.
这是一个有用的阅读:限制 Linux 中程序的时间和内存消耗,这导致了超时工具,它可以让您通过时间或内存消耗来控制进程(及其分支)。
回答by Chris Hinshaw
I have been dealing with this exact problem and have found the best solution if you are using 2.6.24 or higher is to use cgroup / cgroups. An example would be like this, here is the default /etc/cgconfig.conf file with an added control group at the bottom. This control group limits the amout of physical memory to 100MB and the total virtual memory allocation to 200MB.
我一直在处理这个确切的问题,如果您使用 2.6.24 或更高版本,我发现最好的解决方案是使用 cgroup/ cgroups。一个例子是这样的,这里是默认的 /etc/cgconfig.conf 文件,在底部添加了一个控制组。该控制组将物理内存限制为 100MB,将虚拟内存分配总量限制为 200MB。
mount {
cpuset = /cgroup/cpuset;
cpu = /cgroup/cpu;
cpuacct = /cgroup/cpuacct;
memory = /cgroup/memory;
devices = /cgroup/devices;
freezer = /cgroup/freezer;
net_cls = /cgroup/net_cls;
blkio = /cgroup/blkio;
}
group daemons/java_limited_process {
memory {
memory.limit_in_bytes = "104857600";
memory.memsw.limit_in_bytes = "209715200";
}
}
Once I have this configured, I can add process to the group like so
一旦我配置了这个,我就可以像这样将进程添加到组中
cgexec -g memory:daemons/java_limited_process /usr/local/tomcat/bin/startup.sh
This will limit the memory of the main process and any children it spawns. It also has facilities to query memory usage in the controllers.
这将限制主进程及其产生的任何子进程的内存。它还具有查询控制器中内存使用情况的功能。