VSZ vs RSS memory and swap space
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.
Linux RSS and Shared Memory
RSS is the amount of physical memory mapped to your process.
Linux employs demand paging so that physical memory only gets mapped on the first access. VSZ is virtual memory which gets backed by physical memory on demand. This explains why your RSS grows as you access more of the shared memory mapping.
Memory Allocation vs RSS in Linux
No, RSS is not expected to grow after every single allocation.
It's inefficient to keep asking the OS for tiny amounts of memory, so a good allocator will request a larger chunk, and then parcel it out without getting the OS involved.
Additionally, memory is paged in lazily. A large, untouched allocation will not contribute to RSS. (In this particular case, the vector will make sure the memory is initialized, so this is not an issue here, but it could have been if you had allocated it with .reserve(4096)
instead).
This means that you'll instead see memory stay the same for several allocations+initializations in a row, and then suddenly go up. If you keep allocating more data, you'll probably see this effect.
How can I calculate relation between rss and vsz in ps command output bash script?
Good morning, this is related to your usage of the for loop. For splits on every word separated by spaces. For looping on lines, better use while, like this:
#!/bin/bash
ps axo time,vsz,rss,pid,command --no-header | tr -s ' ''_' | sort | while read line
do
echo $line
var1=$(echo $line | cut -d" " -f2)
var2=$(echo $line | cut -d" " -f3)
if [ $var1 -ne 0 ]
then
result=$(echo "$var2 / $var1" | bc -l)
else
result="N/A"
fi
echo $line $result
done
I also added a verification for $var1 == 0. Dividing by 0 is always a problem!
Does linux process VSZ equal 0 mean kernel space application?
VSZ is the Virtual Memory Size. It includes all memory that the process can access, including memory that is swapped out, memory that is allocated, but not used, and memory that is from shared libraries.
So, the top
command screenshot you shared showing VSZ values equaling 0, means that those processes are not using VSZ.
NOTE: They are kernel threads and memory statistics are irrelevant for them as they use kernel memory. Just to visualize kernel processes, press c
when top command is running and it will show you all [bracketed] entries in last column named COMMAND.
You can get more details on VSZ and learn about its counterpart RSS (Resident Set Size) from here.
Related Topics
How to Find All Files Containing Specific Text on Linux
How to Generate a Core Dump in Linux on a Segmentation Fault
What Is the Meaning of So_Reuseaddr (Setsockopt Option) - Linux
How to Use Sed to Change My Configuration Files, With Flexible Keys and Values
Linking Against an Old Version of Libc to Provide Greater Application Coverage
How to Redirect Output of Systemd Service to a File
How to Set Linux Environment Variables With Ansible
Bash Command Not Found When Setting a Variable
Recursively Counting Files in a Linux Directory
Convert Xlsx to CSV in Linux With Command Line
How to Change the Output Color of Echo in Linux
How to Kill a Child Process After a Given Timeout in Bash
How to Merge Two Files Using Awk
Get Destination Address of a Received Udp Packet
How to Ensure Only One Instance of a Bash Script Is Running
How to Find the Last Field Using 'Cut'