How to limit memory allocated to a user’s processes & resolve “memory exhausted” error in Linux?

In this article, we will look how to limit memory usage by a process on Linux system,

Again for this trick, we can use the file /etc/security/limits.conf to pose the limit on the memory a user’s processes can occupy.

1. Make an entry in /etc/security/limits.conf like below:

[root@nglinux ~]# tail -1 /etc/security/limits.conf 
saket        hard    as           42000

What this entry tells ?
a. saket is the user
b. hard tells this entry is the hard limit(i.e. max. limit for the user).
c. as is the address space limit in KB i.e. total address space or memory a user can occupy.
d. And 4200KB is the max limit set i.e. around 40 MB.

2. Now switch to the user on which this limit is imposed.

[root@nglinux ~]# su - saket
Mysql password is root/redhat
PHPMyAdmin can be accessed at http://localhost/phpmyadmin
Apache and FTP Server already setup.
Your IP addr:

NGEL System Console can be accessed at URL http://localhost/system
Run NGELINUX_HARDDISK_INSTALL to create partitions and install this live CD on your system hard disk
Run NGELINUX_INSTALL to copy block by block your ISO file to a USB or Complete Hard disk
[saket@nglinux ~]$

3. Now try to occupy less and more memory than the limit.

[saket@nglinux ~]$ dd if=/dev/zero of=/dev/null bs=30M
^C91+1 records in
91+0 records out
2862612480 bytes (2.9 GB) copied, 1.26322 s, 2.3 GB/s

[saket@nglinux ~]$ dd if=/dev/zero of=/dev/null bs=50M
dd: memory exhausted
[saket@nglinux ~]$ 

We can see the memory can be occupied if it is less than limit and we get an error dd: memory exhausted in case we will try to occupy more memory than the limit.

So from now onwards you can limit memory usage for all users by creating a group and imposing limit on that.

And in case some user demands more memory you can increase it from here.

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments