Is setting ulimit -v sufficient to avoid memory leak

DJ180

We have a process that in recent weeks had a once-off memory leak that resulted in it consuming all memory on our RHEL 7 box

We now wish to set limits around this such that it will never take any more than a certain amount

We are using the ulimit -v setting to set this amount (as the -m setting does not work)

Therefore, I'm wondering if this is sufficient or do we also need a way to limit physical memory as well? IF so, what is the best way to go about this?

If virtual memory always grows with phyiscal memory then perhaps -v by itself is sufficient

Yurij Goncharuk

Some description about how ulimit works:

ulimit has deal with setrlimit and getrlimit system calls. It's easy to ensure by strace-ing of bash process (ulimit is component of the bash). I set 1024kb of max memory size:

$ ulimit -m 1024

In another console:

$ strace -p <my_bash_pid>
. . .
getrlimit(RLIMIT_RSS, {rlim_cur=1024*1024, rlim_max=1024*1024}) = 0
setrlimit(RLIMIT_RSS, {rlim_cur=1024*1024, rlim_max=1024*1024}) = 0
. . . 

setrlimit man page write next about RLIMIT_RSS:

RLIMIT_RSS Specifies the limit (in pages) of the process's resident set (the number of virtual pages resident in RAM). This limit only has effect in Linux 2.4.x, x < 30, and there only affects calls to madvise(2) specifying MADV_WILLNEED.

madvice syscall is just advice to kernel and kernel may ignore this advice. Even bash man page about ulimit write following:

-m The maximum resident set size (many systems do not honor this limit)

That is the reason why -m doesn't work.

About -v option:

I set 1024 kb of virtual memory:

$ ulimit -v 1024

In another console:

$ strace -p <my_bash_pid>
. . .
getrlimit(RLIMIT_AS, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}) = 0
setrlimit(RLIMIT_AS, {rlim_cur=1024*1024, rlim_max=1024*1024}) = 0
. . .

setrlimit man page write next about RLIMIT_AS:

RLIMIT_AS The maximum size of the process's virtual memory (address space) in bytes. This limit affects calls to brk(2), mmap(2) and mremap(2), which fail with the error ENOMEM upon exceeding this limit. Also automatic stack expansion will fail (and generate a SIGSEGV that kills the process if no alternate stack has been made available via sigaltstack(2)). Since the value is a long, on machines with a 32-bit long either this limit is at most 2 GiB, or this resource is unlimited.

Program consist of 3 segments (data, code, stack) compose virtual program memory space.

  • Code segment is const and contain program instructions.

  • Data segment is controlled by following:

    brk syscall adjusts size of data segment (part of virtual memory) of the program.

    mmap syscall maps file or device to virtual memory of process.

    Many programs allocates memory (direct or indirect) by calling standard function from C Library (malloc) which allocates memory from heap (part of data segment). malloc adjust size of data segment by calling brk syscall.

  • Stack stores functions variables (variable takes memory during allocation from stack).

So, that's why the -v option is works for you.

If -v is sufficient for your task, then there is no reasons to do something else and it's sufficient.


If you want to take control about huge count of specific memory features for process (memory pressure, swap usage, RSS limit, OOM and so on) I suggest to you to use cgroups memory capabilities.

If your application is a service I suggest to you to use systemd slice features, as the most convenient for controlling and limiting resources of service or group of services (also it easy to configure instead of configuring cgroups directly) which is managed by systemd.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Avoid Memory leak

From Dev

How to avoid this memory leak?

From Dev

Avoid memory leak with WeakReference Android

From Dev

Although setting TImage::Picture = NULL won't cause a memory leak, what alternatives exist that might avoid that confusion?

From Dev

How to release memory in android to avoid memory leak

From Dev

Soft virtual memory limit (ulimit -v)

From Dev

nodejs setMaxListeners to avoid memory leak detection

From Dev

Ways to avoid memory leak when exception thrown

From Dev

How to avoid a memory leak with __block and completion blocks

From Dev

weak to non class types to avoid memory leak

From Dev

how to avoid memory leak in dynamically allocated widget

From Dev

How can I fix to avoid a memory leak?

From Dev

How can I avoid a memory leak in this function?

From Dev

How to avoid a memory leak with __block and completion blocks

From Dev

Setting pointers to nil to prevent memory leak in Golang

From Dev

Powershell v3 potential memory leak

From Dev

How to avoid memory leak with shared_ptr and SWIG

From Dev

How to avoid memory leak at design-implementation level

From Dev

Node and RxJs: How can I avoid a memory leak on a long process?

From Dev

Weak reference instead of getActivity() (Android avoid memory leak)?

From Dev

How to avoid a memory leak by using pthread_cancel?

From Dev

Node and RxJs: How can I avoid a memory leak on a long process?

From Dev

How to handle unmounting a component with set interval to avoid a memory leak

From Dev

setting ulimit on a running process

From Dev

Setting allowdrop to true cause memory leak in .net application

From Dev

Fragment view memory leak despite setting parent view to null in onDestroyView

From Dev

Java and virtual memory ulimit

From Dev

Java and virtual memory ulimit

From Dev

ulimit not limiting memory usage

Related Related

  1. 1

    Avoid Memory leak

  2. 2

    How to avoid this memory leak?

  3. 3

    Avoid memory leak with WeakReference Android

  4. 4

    Although setting TImage::Picture = NULL won't cause a memory leak, what alternatives exist that might avoid that confusion?

  5. 5

    How to release memory in android to avoid memory leak

  6. 6

    Soft virtual memory limit (ulimit -v)

  7. 7

    nodejs setMaxListeners to avoid memory leak detection

  8. 8

    Ways to avoid memory leak when exception thrown

  9. 9

    How to avoid a memory leak with __block and completion blocks

  10. 10

    weak to non class types to avoid memory leak

  11. 11

    how to avoid memory leak in dynamically allocated widget

  12. 12

    How can I fix to avoid a memory leak?

  13. 13

    How can I avoid a memory leak in this function?

  14. 14

    How to avoid a memory leak with __block and completion blocks

  15. 15

    Setting pointers to nil to prevent memory leak in Golang

  16. 16

    Powershell v3 potential memory leak

  17. 17

    How to avoid memory leak with shared_ptr and SWIG

  18. 18

    How to avoid memory leak at design-implementation level

  19. 19

    Node and RxJs: How can I avoid a memory leak on a long process?

  20. 20

    Weak reference instead of getActivity() (Android avoid memory leak)?

  21. 21

    How to avoid a memory leak by using pthread_cancel?

  22. 22

    Node and RxJs: How can I avoid a memory leak on a long process?

  23. 23

    How to handle unmounting a component with set interval to avoid a memory leak

  24. 24

    setting ulimit on a running process

  25. 25

    Setting allowdrop to true cause memory leak in .net application

  26. 26

    Fragment view memory leak despite setting parent view to null in onDestroyView

  27. 27

    Java and virtual memory ulimit

  28. 28

    Java and virtual memory ulimit

  29. 29

    ulimit not limiting memory usage

HotTag

Archive