# Limit memory per process

## dylan_stark

Is there a way to enforce limit on memory consumption per process?

I tried to set it with ulimit but it has no effect as far as I can see.

```
ulimit -m 5000
```

but some of processes are taking way more RES memory than 5MB.

----------

## ferreirafm

Hi dylan, 

FYI,  ulimit is used to set or report a file size limit. I think that's not what you want to do. Does not make sense to limit the total amount of memory of a process, since is done by you CPU and your memory itself. 

Just by chance, do you have any reasons to do that?  

G'Luck

Fred

----------

## Hu

 *dylan_stark wrote:*   

> Is there a way to enforce limit on memory consumption per process?
> 
> I tried to set it with ulimit but it has no effect as far as I can see.
> 
> ```
> ...

 Are those processes children of the shell which set the limit?  Did you set a hard limit or a soft limit?  If the limit is soft, it can be raised back up to the hard limit.  Privileged processes can change their hard and soft limits at will.

 *ferreirafm wrote:*   

> FYI,  ulimit is used to set or report a file size limit.

 That is one of several things that can be influenced by resource limits.  The command showed should have affected max memory size. *ferreirafm wrote:*   

> Does not make sense to limit the total amount of memory of a process, since is done by you CPU and your memory itself.

 No, there are good reasons for limiting the maximum memory of a process.  The kernel only limits memory availability to the extent that it must in order to keep the system functioning.  A user might have a better idea about when a process has exceeded sane levels.

----------

## dylan_stark

The reason for this is my web server which sometimes gets overloaded with PHP processes which somehow run away and consume all the memory on the server.

@Hu

I've set it from BASH after the Apache process was started. Also, I had to set soft limit first and that hard because other way around was giving me error.

----------

## ferreirafm

Hi dylan, 

You have to figure out how to limit the resources consumption of your server.

Unlimit is set by shell. If you set it globally, you probably are going to have problems in writing files of other process. Limit the size of your files is not going to solve your problem. I guess that the size of your files has noting to do with your problem. Do you have such a huge files??

G'Luck 

FredLast edited by ferreirafm on Wed Sep 29, 2010 8:02 pm; edited 1 time in total

----------

## Mousee

Rather than set anything globally, or otherwise, why don't you just modify the Apache init script to include your ulimit set?

In this way it would only affect the Apache process (and I assume children, haven't used it for Apache so I may be wrong) and nothing else.

----------

## tomk

If it's PHP that's causing you troubles you should edit the memory_limit variable in /etc/php/apache2-php5/php.ini which is the amount of memory each PHP script can use.

----------

## cach0rr0

might also be worth fiddling with MPM settings

check the comments in  /etc/apache2/modules.d/00_mpm.conf

----------

