Dear misc@

I have searched the archives and read the documentation of login.conf(5),
ksh(1):ulimit and can not find how to limit the amount of physical memory a
process may use.

I have the following limits where I have set down ulimit -m and ulimit -l
to 10000 kbytes in an attempt to limit the process I spawn which is
the Erlang VM.

$ ulimit -a
time(cpu-seconds)    unlimited
file(blocks)         unlimited
coredump(blocks)     unlimited
data(kbytes)         33554432
stack(kbytes)        8192
lockedmem(kbytes)    10000
memory(kbytes)       10000
nofiles(descriptors) 1024
processes            1024

Note that the machine has got 8 GB of physical memory and 8 GB of swap and
that I have set datasize=infinity in /etc/login.conf. I got
datasize=33554432 which seems to be the same as kern.shminfo.shmmax.
The datasize is twice the physical memory + swap.

Then I start the Erlang VM and tell it to allocate an address block of 30000
MByte for future use where it will store all literal data in the same block
(this is a garbage collector optimization).  Not much of this data is
actually used.

 68196 beam     CALL  
mmap(0,0x753000000,0<PROT_NONE>,0x1002<MAP_PRIVATE|MAP_ANON>,-1,0)
 68196 beam     RET   mmap 11871265173504/0xacbfe8b3000

Note the protection flags on the block.  No access is allowed.  This trick
works just fine; here is what top says:

load averages:  0.15,  0.13,  0.09         frerin.otp.ericsson.se 08:49:46
48 processes: 47 idle, 1 on processor                             up 13:49
CPU0 states:  0.0% user,  0.0% nice,  0.0% system,  0.0% interrupt,  100% idle
CPU1 states:  0.0% user,  0.0% nice,  0.0% system,  0.0% interrupt,  100% idle
Memory: Real: 43M/636M act/tot Free: 7028M Cache: 508M Swap: 0K/8155M

  PID USERNAME PRI NICE  SIZE   RES STATE     WAIT      TIME    CPU COMMAND
68196 raimo      2    0   29G   15M sleep     poll      0:00  1.42% beam

So I have a process with a data size of 29 GB on a machine with 16 GB
memory + swap.  I have also tried to start an additional Erlang VM that
also allocates 29 GB of virtual memory which also works.

That this is allowed is just fine for me - this trick of allocating a
"large enough" PROT_NONE memory to get one address range for some special
data type is very useful for the Erlang VM.  But I wonder how to limit the
actual memory use?  Setting down ulimit -m and ulimit -l to 10000 kbytes
did not prevent this process from getting 15 MByte of "RES" memory...

Is there some way to limit the actual amount of memory for a process when I
need to set up the datasize to allow for large unused virtual memory
blocks?

dmesg | head -4:
OpenBSD 6.0 (GENERIC.MP) #1: Fri Sep 23 08:53:49 CEST 2016
    
r...@stable-60-amd64.mtier.org:/binpatchng/work-binpatch60-amd64/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 8283037696 (7899MB)
avail mem = 8027529216 (7655MB)

Best Regards
-- 
/ Raimo Niskanen, Erlang/OTP, Ericsson AB

Reply via email to