5

I am trying to understand the memory usage of a large scale simulation that we are trying to run. When I run it "ps" reports

USER    PID %CPU %MEM     VSZ    RSS TTY    STAT START   TIME COMMAND
myuser 5252 97.7  0.5 5751412 377392 ?      Rs   19:49   1:15 myprogram

We have three arrays in that simulation that each occupy 1.6gb (200 million doubles). Based on the information in

What is RSS and VSZ in Linux memory management

I expected that memory to be listed under RSS, but RSS is only 377MB. Based on the information in the stackoverflow thread I concluded that the memory must be swapped out and looked at "free -m"

             total       used       free     shared    buffers     cached
Mem:         64391       5985      58406          0        463       1295
-/+ buffers/cache:       4226      60164
Swap:         4766          0       4766

and swap is not used at all! Aside from the fact that it is too small anyway. So where is this difference in RSS vs VSZ coming from? Why are the arrays that we allocate part of VSZ and not part of RSS?

I appreciate all help

Community
  • 1
  • 1
ftiaronsem
  • 1,524
  • 4
  • 19
  • 32
  • Has the program actually touched (read or write) the entries in those array? A process is allowed to "allocate" as much memory as it has available address space for - reguardless of the amount of ram/swap available on the machine. Actual memory isn't allocted to the process until the process actually touches the memory. If the OS doesn't have any memory available at that time, then the process is instantly killed. Try looking at "VSIZE", also make your program set each of those arrays to some specific value, like 0, just after allocation. – joeking Aug 07 '15 at 01:34
  • Thanks a lot for your comment that was already really helpful. And you are right, the program hasn't touched these arrays at that time yet. And later on the arrays are initialized through a loop, that doesn't even touch all array elements. Do you know what happens to RSS and VSZ in that case? Let's say the loop only loops over the first half of the array. Will RSS only increase by 800MB? That would be incredibly useful to know. – ftiaronsem Aug 07 '15 at 20:54

1 Answers1

6

Simple answer to your question is that arrays are defined in virtual space thats why memory for array is shown in VSZ only when you will use array it wil become part of RSS. in my view by keeping your thinking simple will give you explanation. VSZ is virtual memory which a process can use while RSS is physical memory actually allocated at the moment. When a virtual memory is actually used OS will allocate the memory which will increase the RSS.

incompetent
  • 1,715
  • 18
  • 29
  • Thanks a lot for your answer, it already helped a lot. Do you happen to know anything about the situation I described in my reply to joeking's comment? – ftiaronsem Aug 07 '15 at 20:56
  • @ftiaronsem here comes in swap play if your OS is comfortable in keeping all pages in RAM RSS will increase 800MB but in my view you can not predict that in prior. – incompetent Aug 08 '15 at 01:35
  • 1
    Sure, RSS will increase proportionally to the amount of the array accessed. However, it is allocated in page-size blocks. I don't know the page size on your system but its probably 4KB or 8KB. (It will be a power of two). Touch any byte in a 4KB page and the entire page is committed to memory (RSS). – joeking Aug 09 '15 at 00:25
  • @joeking does RSS contains the pages present in swap area? – incompetent Aug 09 '15 at 06:25
  • see: http://stackoverflow.com/questions/7880784/what-is-rss-and-vsz-in-linux-memory-management – joeking Aug 10 '15 at 14:07