0

To limit memory resource for particular process we can use ulimit as well as cgroup.

I want to understand that if using cgroup, I have allocated say ~700 MB of memory to process A, on system having 1 GB of RAM, and some other process say B, requires ~400 MB of memory. What will happen in this case?

  1. If process A is allocated ~750 MB of memory but using only 200 MB of memory, will process B can use memory that allocated to A?
  2. If no then how to achieve the scenario when "fix amount of memory is assigned to a process that other process can not use"?

EDIT

Is it possible to lock physical memory for process? Or only VM can be locked so that no other process can access it?

There is one multimedia application that must remain alive and can use maximum system resource in terms of memory, I need to achieve this.

Thanks.

SD.
  • 1,432
  • 22
  • 38
  • What is the concrete scenario? What kind of application are you using and asking the question for? **Why do you ask?** – Basile Starynkevitch Nov 16 '15 at 09:19
  • @BasileStarynkevitch: The scenario is, for example on system with 1 GB RAM, I want to allocate ~700 MB of memory that can exclusively be used by that process only. For all the other processes remaining ~300 MB can be used. – SD. Nov 16 '15 at 09:28
  • But that does not tell about the application or program you are running. In general your question is much too abstract. I guess that careful use of `ulimit`, `renice`, `ionice` should be enough – Basile Starynkevitch Nov 16 '15 at 09:28
  • There is one multimedia application that must remain alive and can use maximum system resource in terms of memory, I need to achieve this. – SD. Nov 16 '15 at 09:30
  • You should edit your question, not comment it – Basile Starynkevitch Nov 16 '15 at 09:31
  • Though i am not getting the exact idea, will check and update. – SD. Nov 16 '15 at 09:40

2 Answers2

1

Processes are using virtual memory (not RAM) so they have a virtual address space. See also setrlimit(2) (called by ulimit shell builtin). Perhaps RLIMIT_RSS & RLIMIT_MEMLOCK are relevant. Of course, you could limit some other process e.g. using RLIMIT_AS or RLIMIT_DATA, perhaps thru pam_limits(8) & limits.conf(5)

You could lock some virtual memory into RAM using mlock(2), this ensures that the RAM is kept for the calling process.

If you want to improve performance, you might also use madvise(2) & posix_fadvise(2).

See also ionice(1) & renice(1)

BTW, you might consider using hypervisors like Xen, they are able to reserve RAM.

At last, you might be wrong in believing that your manual tuning could do better than a carefully configured kernel scheduler.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • `mlock` will prevent memory from being paged to the swap area, In my system i have swap space turned off. – SD. Nov 16 '15 at 09:14
  • 1
    Not only swap, it prevent memory from being paged out (e.g. to a file). And I believe you should have some swap space (since the overall performance of the system would then be improved) – Basile Starynkevitch Nov 16 '15 at 09:15
  • 1
    Thanks, but i have application in executable form, no source code, Now only thing i can do is externally restrict resource limit. – SD. Nov 16 '15 at 09:18
  • I guess that Xen would be appropriate. – Basile Starynkevitch Nov 16 '15 at 09:19
  • I'm sure that combining the several ideas in my answer is enough for your particular case – Basile Starynkevitch Nov 16 '15 at 09:34
  • 1
    @SD.: you might be able to hook some library calls fairly easily if the binary you have is dynamically linked. Set `LD_PRELOAD` to the path to a library you've made that does extra stuff on top of `malloc`, or something. There's a lot you can do, but nothing super-useful comes to mind right away. – Peter Cordes Nov 16 '15 at 10:19
0

What other processes will run on the same system, and what kind of thing do you want to happen if the other multimedia program needs memory that other processes are using?

You could weight the multimedia process so the OOM killer only picks it as a last choice after every other non-essential process. You might see a dropped frame if the kernel takes some time killing something to free up memory.

According to this article, adjust the oom-killer weight of a process by writing to /proc/pid/oom_adj. e.g. with

echo -17 > /proc/2592/oom_adj
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847