Have you ever wondered how New Relic calculates the percentage of available memory, we’ve got you covered. Here’s how it works:
The infrastructure agent uses
cat /proc/meminfo to retrieve memory metrics from your server.
Here’s an example of what it returns:
$ cat /proc/meminfo MemTotal: 997820 kB MemFree: 74028 kB MemAvailable: 337608 kB Buffers: 17720 kB Cached: 366776 kB SwapCached: 9500 kB Active: 358876 kB Inactive: 305120 kB Active(anon): 119956 kB Inactive(anon): 172352 kB Active(file): 238920 kB Inactive(file): 132768 kB Unevictable: 32 kB Mlocked: 32 kB SwapTotal: 1046524 kB SwapFree: 428956 kB Dirty: 1452 kB Writeback: 0 kB
With this information, the agent calculates memory metrics in 2 possible ways, depending on the version of the kernel.
On kernels version 3.14+, a new metric was introduced (
MemAvailable) which provides an estimate of how much memory is available for starting new applications, without swapping.
If this metric is available the agent will calculate memory % as follows:
UsedMemory = (MemTotal - MemAvailable) UsedPercentage = (MemTotal - MemAvailable) / MemTotal * 100.0
On older kernels prior to 3.14, we take into account the amount of available memory plus buffers and cached:
AvailableMemory = MemFree + MemBuffers + MemCached and the same calculation is then applied: UsedMemory = (MemTotal - AvailableMemory) UsedPercentage = (MemTotal - AvailableMemory) / MemTotal * 100.0
The reason for this is that Linux considers Buffers and Cache as written memory, however it still considers them as available, so New Relic does too. They are considered available because the system should store frequently accessed data as buffers/cache so that it doesn’t have to retrieve them from the hard drive every time they are needed. However, if a process needs more memory, all of these memory types are promptly surrendered to the process just as if they were not written.
For more information on this, please see this external documentation:
- Computerworld: Making sense of memory usage on Linux
- Linux Ate My RAM - An FAQ on RAM usage in Linux
What about my Windows Server???
In windows the Infrastructure agent does a direct system call to the exported method
It then reads
ullAvailPhys for Total and Available memory values respectively as well as
dwMemoryLoad for the percentage of used memory
For more information on check this MSDN article:
I hope that helps to explain how memory usage is calculated in New Relic Infrastructure!