PDA

View Full Version : Formula to calculate memory free



minhtuanmt1
17-Jun-2013, 07:48
Hi,

I'm a new comer. I'm not sure if I post my question in the right place but if it is please help me to change to the right box.
I have a question about how to calculate the amount of memory free in SUSE. With my understanding:
Total mem free = Memory free + Swap free + Buffer + Cached (I assume Swap is free memory as well in my calculation).
I also found this link in the internet http://blog.thewebsitepeople.org/2011/03/linux-memory-leak-detection/ which state that:
Buffers + Cached = Active(file) + Inactive(file) + ?X?.
Thus, my formula would become:
Total mem free = Memory free + Swap free + Active(file) + Inactive(file) + ?X?.

1/ As the above link is not an official link from SUSE, I can not prove if the formula is correct. Do any one have any idea about my calculation?
2/ What does ?X? in the above formula stand for?

3/ I also noticed that tmpfs is used in my system, about 101MB, please see example below. So the total of memory free must exclude tmpfs. Is my calculation correct for this case as well? If not, any suggestion?

I would be very appreciated if someone could clarify my confuse as I hardly find these kind of information in the internet.

Thanks and Regards,
Tuan

================================================== =============
control1:/dev/shm # df -kh
Filesystem Size Used Avail Use% Mounted on
rootfs 4.0G 572M 3.2G 15% /
/dev/sda7 4.0G 572M 3.2G 15% /
/dev/sda2 4.0G 145M 3.7G 4% /var/log
devtmpfs 458M 124K 458M 1% /dev
tmpfs 499M 101M 399M 21% /dev/shm
shm 499M 101M 399M 21% /dev/shm
/dev/sda1 4.0G 219M 3.6G 6% /boot
/dev/mapper/cluster_vg-data_lv 2.0G 578M 1.4G 31% /.cluster
192.168.0.100:/.cluster 2.0G 578M 1.4G 31% /cluster
control1:/dev/shm # free -m
total used free shared buffers cached
Mem: 997 309 687 0 18 184
-/+ buffers/cache: 106 890
Swap: 2055 0 2055
control1:/dev/shm # cat /proc/meminfo
MemTotal: 1021036 kB
MemFree: 703472 kB
Buffers: 19436 kB
Cached: 180996 kB
SwapCached: 0 kB
Active: 74444 kB
Inactive: 148148 kB
Active(anon): 22256 kB
Inactive(anon): 102432 kB
Active(file): 52188 kB
Inactive(file): 45716 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 2104508 kB
SwapFree: 2104508 kB
Dirty: 4 kB
Writeback: 0 kB
AnonPages: 22228 kB
Mapped: 10092 kB
Shmem: 102528 kB
Slab: 32600 kB
SReclaimable: 8136 kB
SUnreclaim: 24464 kB
KernelStack: 1152 kB
PageTables: 2872 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 2615024 kB
Committed_AS: 260004 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 8884 kB
VmallocChunk: 34359726700 kB
HardwareCorrupted: 0 kB
AnonHugePages: 2048 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 114624 kB
DirectMap2M: 933888 kB
================================================== =============

minhtuanmt1
09-Jul-2013, 03:19
Hi,

I'm a new comer. I'm not sure if I post my question in the right place but if it is please help me to change to the right box.
I have a question about how to calculate the amount of memory free in SUSE. With my understanding:
Total mem free = Memory free + Swap free + Buffer + Cached (I assume Swap is free memory as well in my calculation).
I also found this link in the internet http://blog.thewebsitepeople.org/2011/03/linux-memory-leak-detection/ which state that:
Buffers + Cached = Active(file) + Inactive(file) + ?X?.
Thus, my formula would become:
Total mem free = Memory free + Swap free + Active(file) + Inactive(file) + ?X?.

1/ As the above link is not an official link from SUSE, I can not prove if the formula is correct. Do any one have any idea about my calculation?
2/ What does ?X? in the above formula stand for?

3/ I also noticed that tmpfs is used in my system, about 101MB, please see example below. So the total of memory free must exclude tmpfs. Is my calculation correct for this case as well? If not, any suggestion?

I would be very appreciated if someone could clarify my confuse as I hardly find these kind of information in the internet.

Thanks and Regards,
Tuan

================================================== =============
control1:/dev/shm # df -kh
Filesystem Size Used Avail Use% Mounted on
rootfs 4.0G 572M 3.2G 15% /
/dev/sda7 4.0G 572M 3.2G 15% /
/dev/sda2 4.0G 145M 3.7G 4% /var/log
devtmpfs 458M 124K 458M 1% /dev
tmpfs 499M 101M 399M 21% /dev/shm
shm 499M 101M 399M 21% /dev/shm
/dev/sda1 4.0G 219M 3.6G 6% /boot
/dev/mapper/cluster_vg-data_lv 2.0G 578M 1.4G 31% /.cluster
192.168.0.100:/.cluster 2.0G 578M 1.4G 31% /cluster
control1:/dev/shm # free -m
total used free shared buffers cached
Mem: 997 309 687 0 18 184
-/+ buffers/cache: 106 890
Swap: 2055 0 2055
control1:/dev/shm # cat /proc/meminfo
MemTotal: 1021036 kB
MemFree: 703472 kB
Buffers: 19436 kB
Cached: 180996 kB
SwapCached: 0 kB
Active: 74444 kB
Inactive: 148148 kB
Active(anon): 22256 kB
Inactive(anon): 102432 kB
Active(file): 52188 kB
Inactive(file): 45716 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 2104508 kB
SwapFree: 2104508 kB
Dirty: 4 kB
Writeback: 0 kB
AnonPages: 22228 kB
Mapped: 10092 kB
Shmem: 102528 kB
Slab: 32600 kB
SReclaimable: 8136 kB
SUnreclaim: 24464 kB
KernelStack: 1152 kB
PageTables: 2872 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 2615024 kB
Committed_AS: 260004 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 8884 kB
VmallocChunk: 34359726700 kB
HardwareCorrupted: 0 kB
AnonHugePages: 2048 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 114624 kB
DirectMap2M: 933888 kB
================================================== =============

Could anyone help me please?

jmozdzen
09-Jul-2013, 10:12
Hi Tuan,

sorry, somehow your question has slipped by - usually I jump at this type of posts ;)

> how to calculate the amount of memory free in SUSE

Please define "memory free" :)

Because of the memory model, especially the impact of virtual memory, there are more than one definition, and each answer would actually serve a purpose (and not be purely academical):

1) free physical memory ("MemFree: 703472 kB")
This value is optimized to "almost zero" on Linux systems, as "free physical memory" can serve a better purpose than to idle away... most easily it can be used as buffer/cache space for physical (disk) I/O... if you have a lot of free physical memory, either something is wrong or you have (had) less disk i/o than available free physical memory, IOW a system that is pretty much idle ;)

2) physical memory available to applications (MemFree+Buffers+Cached, minus x%)
Linux will automatically shrink the I/O buffer (but not to zero) rather than using virtual memory. So the memory your applications can use up, without causing swapping, is the sum of already free physical memory plus the memory that can be freed from buffers and cache.
Please note that this may still have some impact on performance: As less space is available for I/O buffers, I/O-intensive tasks will notice waiting time when accessing disks.

3) memory available to applications ( SwapFree + (MemFree+Buffers+Cached) * 0,x )
Once the physical memory is used up, the memory manager will try to page out memory to (slower) storage. Commonly called "swapping", but actually it is "paging"... just to have it mentioned.
When your system starts paging, things usually get slow. And if you have much paging, things may slow down almost to a halt. It's something to avoid ;)

"active" and "inactive" refers to the state of swapped/cached memory: If that memory has not been modified since reading from disk, it can be freed if memory is tight, and is called "inactive". Once the content is changed, it needs to be written to disk before freeing that piece of memory, and called "active".

I'm sure you're after some specific answer and probably not just trying to understand the mechanics of Linux memory management. As there are many tunables in this area, it would be better to know some background, in order to give you properly matching answers.

Regards,
Jens

minhtuanmt1
11-Jul-2013, 04:27
Hi Tuan,

sorry, somehow your question has slipped by - usually I jump at this type of posts ;)

> how to calculate the amount of memory free in SUSE

Please define "memory free" :)

Because of the memory model, especially the impact of virtual memory, there are more than one definition, and each answer would actually serve a purpose (and not be purely academical):

1) free physical memory ("MemFree: 703472 kB")
This value is optimized to "almost zero" on Linux systems, as "free physical memory" can serve a better purpose than to idle away... most easily it can be used as buffer/cache space for physical (disk) I/O... if you have a lot of free physical memory, either something is wrong or you have (had) less disk i/o than available free physical memory, IOW a system that is pretty much idle ;)

2) physical memory available to applications (MemFree+Buffers+Cached, minus x%)
Linux will automatically shrink the I/O buffer (but not to zero) rather than using virtual memory. So the memory your applications can use up, without causing swapping, is the sum of already free physical memory plus the memory that can be freed from buffers and cache.
Please note that this may still have some impact on performance: As less space is available for I/O buffers, I/O-intensive tasks will notice waiting time when accessing disks.

3) memory available to applications ( SwapFree + (MemFree+Buffers+Cached) * 0,x )
Once the physical memory is used up, the memory manager will try to page out memory to (slower) storage. Commonly called "swapping", but actually it is "paging"... just to have it mentioned.
When your system starts paging, things usually get slow. And if you have much paging, things may slow down almost to a halt. It's something to avoid ;)

"active" and "inactive" refers to the state of swapped/cached memory: If that memory has not been modified since reading from disk, it can be freed if memory is tight, and is called "inactive". Once the content is changed, it needs to be written to disk before freeing that piece of memory, and called "active".

I'm sure you're after some specific answer and probably not just trying to understand the mechanics of Linux memory management. As there are many tunables in this area, it would be better to know some background, in order to give you properly matching answers.

Regards,
Jens

Hi Jens,

Thanks a lot for your clarification. My question is about free memory from application point of view. That's your #3 item.
The fact is that I want to remove tmpfs file out of my calculation. For example: if I create a 100MB tmpfs file i.e in my case it is under /dev/shm, this file will either be stored in cached or swap-used and cached or swap-used will increase 100MB. These files under /dev/shm can not be moved to anywhere else if we are running out of physical mem and swap, thus I want to exclude tmpfs out of my formula. In other words, my formula would be:
total free memory = mem_free + swap_free + active_file + inactive_file

A hint from http://blog.thewebsitepeople.org/2011/03/linux-memory-leak-detection/ is all I have. I'm not sure if my formula is usable.

Thanks,
/Tuan

jmozdzen
11-Jul-2013, 09:27
Hi Tuan,

for your purpose, I'd say use the "free" command and add up the values from the "free" column, row 2 and 3 (free physical memory after considering buffers and caches in line 2, free swap space on line 3).

Regards,
Jens

PS: A very short, end-user oriented description can be seen on http://www.linuxatemyram.com/ - I like that table comparing users' to system view of the term "free" :D