Skip to content

Setting limit to total physical memory available in Linux

I know that I am supposed to set mem=MEMORY_LIMIT. But I do not know where to go, during runtime, or during boot time, in order to set a limit to the total physical memory that the OS has control of.

I am running I/O benchmarks, and I would like to limit the amount of overall physical memory that is available.
share improve this question
asked Nov 20 '12 at 23:30
KZcoding
718●22 gold badges●99 silver badges●2323 bronze badges
edited Apr 5 '15 at 4:37
jdphenix
10.9k●33 gold badges●3232 silver badges●6060 bronze badges
5 Answers
order by
up vote
15
down vote
accepted

I found the answer I was looking for. Basically, the parameter that sets the total available physical memory is "mem=MEMORY_LIMIT". And this is a kernel boot parameter. You need to add, say "mem=1G" for maximum of 1GB available physical memory to the kernel boot parameter. For more info on how to add kernel boot parameters look at https://wiki.ubuntu.com/Kernel/KernelBootParameters
share improve this answer
answered Nov 20 '12 at 23:43
KZcoding
718●22 gold badges●99 silver badges●2323 bronze badges

    The parameter has a slightly different meaning for x86 systems (both 32 and 64 bit): "mem=nn[KMG] […] [X86] Work as limiting max address" (source). Due to reserved memory sections, its value on x86 will be higher than the amount of memory to make available to the OS. Example: Had to use mem=6700M to get 5365 out of 6144 MiB (6 GiB) physically available memory. – tanius Aug 20 '16 at 19:18 

add a comment
up vote
6
down vote

Edit your kernel boot parameters in lilo.conf, grub.conf, grub.cfg, or menu.lst (which one depends on your particular distro and bootloader; check your distro's documentation for more detail) to include the parameter mem=512M (or whatever size you want to emulate) on the line specifying your kernel parameters.

For instance, in Grub, there should be a line that says something like kernel /boot/vmlinuz param1=val1 param2=val2. Add the mem=512M to that list of parameters. You can create separate entries for your boot menu by copying these entire definitions, renaming them, and configuring each with a different amount of memory, so you can quickly boot with different settings.
share improve this answer
answered Nov 20 '12 at 23:42
Brian Campbell
245k●5252 gold badges●324324 silver badges●321321 bronze badges
up vote
3
down vote

To add to Brian Campbell's list, for the uBoot bootloader on the BeagleBone / Black devices, edit the kernel paramters in /boot/uboot/uEnv.txt
Add or modify the line mmcargs=setenv bootargs mem=512M [tested with Debian]

Use > free before and after [reboot] to confirm the modification
share improve this answer
answered Jul 23 '14 at 20:16
TekuConcept
895●99 silver badges●1616 bronze badges
up vote
0
down vote

A1: Yes, you have to reboot.

A2: The kernel is rather unforgiving with respect to typos. There are no error messages. Could that be your problem? Examples: " mem=512M" and " mem=2G". Don't forget the space following the previous parameter, mem is in lower case, and K, M or G are upper case.
share improve this answer
answered Aug 8 '17 at 10:31
George Rolf
1
up vote
0
down vote

I've followed the instructions in this page that KZcoding mentioned (part: Permanently Add a Kernel Boot Parameter)

My Linux is (vm in virtualbox):

Linux ubuntu 4.4.0-21-generic #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

Before setting:

yavuz@ubuntu:~$ free -m
          total        used        free      shared  buff/cache   available
Mem:           3630         305        3010           6         314        3264
Swap:          3767           0        3767

Just changed this line in /etc/default/grub

GRUB_CMDLINE_LINUX_DEFAULT=""

to

GRUB_CMDLINE_LINUX_DEFAULT="mem=2G"

then restart the server. After restart:

yavuz@ubuntu:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:           2000         298        1385           4         315        1651
Swap:          3767           0        3767

share improve this answer
answered Jul 1 '17 at 10:34
Yavuz Sert
954●11 gold badge●88 silver badges●1818 bronze badges
edited Nov 15 '17 at 7:32

    I suppose we need to reboot in order to see the change in available memory? – Stephane Jul 8 '17 at 9:15
    On my Ubuntu 16.04 even after a reboot, the available memory hasn't changed. – Stephane Jul 8 '17 at 9:21
    @Stephane did you run sudo update-grub after editing /etc/default/grup? – Yang Nov 13 '17 at 6:59
    @Yang I solved my issue a long time ago by switching back the mem sticks to the original sticks. – Stephane Nov 13 '17 at 15:24
    thank you @Stephane, i've edited the answer and added that we have to restart the server. – Yavuz Sert Nov 15 '17 at 7:33

https://stackoverflow.com/questions/13484016/setting-limit-to-total-physical-memory-available-in-linux