9

I'm trying to figure out a good formula for finding out how much memory is available. I'm using the following formula currently: freeMem = MemFree + Buffers + Cached - Shmem. However, according to this formula my embedded system is losing memory. Now I'm wondering if I have a memory leak so I enabled kmemleak in the kernel. According to mpatrol, valgrind, and coverity I do not have any leaks in user space. Is there a leak in kernel space or is my formula off? Note that I do not have any swap for this device.

MYBOX> cat /proc/meminfo
MemTotal:        2073348 kB
MemFree:         1388180 kB
Buffers:          137016 kB
Cached:            88772 kB
SwapCached:            0 kB
Active:           589124 kB
Inactive:          44380 kB
Active(anon):     410236 kB
Inactive(anon):     1992 kB
Active(file):     178888 kB
Inactive(file):    42388 kB
Unevictable:           0 kB
Mlocked:               0 kB
HighTotal:       1310716 kB
HighFree:         811828 kB
LowTotal:         762632 kB
LowFree:          576352 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                64 kB
Writeback:             0 kB
AnonPages:        407712 kB
Mapped:            26140 kB
Shmem:              4516 kB
Slab:              40408 kB
SReclaimable:       8320 kB
SUnreclaim:        32088 kB
KernelStack:        1480 kB
PageTables:         1464 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     1036672 kB
Committed_AS:     660508 kB
VmallocTotal:     237344 kB
VmallocUsed:      104556 kB
VmallocChunk:     126296 kB

6 Answers 6

2

Leaking memory from userland would not show anyway in /proc/meminfo because as far as the kernel is concerned it is memory allocated (no matter if you use free() in your userland app or not, it's either allocated with the mmap() syscall or brk()/sbrk() and the kernel keeps track of the pages allocated in userland, otherwise we would be in serious trouble ;).

I don't clearly understand how you came up with your belief that your are leaking memory? Here is a good link redhat/meminfo if you haven't read it already which explains what each statistic really means.

5
  • Well I was monitoring the system while it was idle over a two week period. The system was down about 10MB over that week - this may not seem like much but the system needs to run for years without a reboot. I'm pretty sure the the applications are clean but I was wondering about the drivers I am using. Wouldn't /proc/meminfo show me the whole system user and kernel space?
    – atomicbaum
    Dec 1, 2011 at 23:25
  • 1
    Yep /proc/meminfo would show the whole thing, but you cannot make an equation from this to guess leaked memory. On the other hand you can detect than more and more memory is being used, if it is what you are saying. In this case, instead of trying to make your own equation, you can use the free -m command as explained here: blog.scoutapp.com/articles/2010/01/11/…. If you still notice the available memory going down and down there is definitively something eating more and more memory (but not necessarily leaking memory ;) Dec 1, 2011 at 23:53
  • I don't know why I didn't look at free before... Looking at the source for the "free" command it looks like the formula is "kb_main_free + kb_main_buffers + kb_main_cached" and it does grab that does come from /proc/meminfo... (looking at sysinfo.c and free.c).
    – atomicbaum
    Dec 2, 2011 at 0:26
  • So are you saying that you have noticed your available free memory decreasing? Because from your question I understood that you could not find some part of the memory by summing your freeMem and the used memory. You have your answered that your equation is correct for the freeMem, how about the one you are using for the used memory? What exactly are you noticing? Dec 2, 2011 at 0:37
  • I'm noticing a slow decline in memory with a system sitting there. Something COULD be growing in memory slowly or there could just be a tiny memory leak somewhere. I lose about 10MB a week.
    – atomicbaum
    Dec 2, 2011 at 0:42
2

Your "free memory" calculation is missing one thing - it should be adding SReclaimable (for the reclaimable slab cache) to the effectively-free memory.

If this does not change the slow reduction in effectively-free memory over time, you should take snapshots of /proc/meminfo at regular intervals and identify which line shows the increase.

If it is the SUnreclaim line that is increasing, you can look in /proc/slabinfo to see your kernel slab usage and identify the culprit. It is possible that it is simply memory fragmentation that you are observing, and it will eventually settle down over a longer time period.

2
  • 1
    re: settling down, are you saying that a growing SUnreclaim amount is OK? or sometimes OK, mostly bad, or always bad. I've got a device with growing SUnreclaim, reaching 60% of physical ram. And tasks that work on a freshly booted system fail to work (and trigger the OOM killer and reboots) when SUnreclaim has gotten that high. (it's very hard to find much info about this value)
    – Karl P
    May 4, 2012 at 13:06
  • @KarlP: It's entirely dependent on the context, just like a large amount of userspace memory isn't neccesarily a good or bad thing. If you need the memory for something else (and it seems like you do), then you probably want to identify why the memory is being consumed by the slab cache - check /proc/slabinfo as a first step.
    – caf
    May 4, 2012 at 13:41
0

Agreed with auxv - using /proc/meminfo is probably not the best way of tracking user process memory, as it includes memory allocated by all user processes making it difficult to narrow down the consumption of your process.

A better way to track the total memory consumed by your process would be to use top (1) and look at VIRT (which includes memory swapped out) or RES (which only includes physical memory).

But if you do want to use /proc/meminfo then the formula I would use would be:

MemTotal = MemFree + Cached + Buffers + SwapCached

...note that this only accounts for data, not code. Most of MemTotal - (quantity on the right of the equation) should be your kernel image.

7
  • It's not that it's not the best way, it's just impossible to track memory leak in userland from the kernel POV. From a userland application a leak is when you lose any pointer referencing a memory area without calling free() before hand. But for the kernel, this memory is still referenced as used memory and would then appear as used memory (i.e not appearing as a hole in OP's equation). IMHO /proc/meminfo just does not tell anything about leaked memory. You can't really guess leaked memory with top either as the leaked memory would appear as regular VIRT mem.(again no hole in the equation) Dec 1, 2011 at 23:15
  • Sorry the comment was too long... However I do agree with you that OP's hole, which is (wrongly) interpreted as leaked memory is mostly the kernel image code and some other few bits reserved by the kernel (explained on the link on my answer). Dec 1, 2011 at 23:17
  • The system doesn't have any swap, but I would like to know how much memory is available for the whole system. I'm not sure what you mean by "this only accounts for data, not code". Are you talking about loaded programs in RAM?
    – atomicbaum
    Dec 1, 2011 at 23:28
  • auxv: I see what you mean, but i would still say hard not impossible. You can make decent headway through statistics, e.g. see: usenix.org/event/osdi08/tech/full_papers/bhatia/bhatia_html atomicbaum: I was talking about the kernel binary (vmlinux).
    – er0
    Dec 2, 2011 at 2:09
  • @er0 Quoting your link, "Choptix continuously collects profiles of low-level OS events [...] at the granularity of executables, procedures and instructions" i.e not at all the granularity that /proc/meminfo gives you. Dec 3, 2011 at 19:54
0

For my systems I am using the following command to check how much memory is consumed:

ps aux | awk '{sum +=$4}'

This adds the total percentage of used memory for all processes that are currently running.

0

FreeMem = MemFree + Buffers + Cached - Mapped, The cached memory contains the mapped part, this part have been mapped to user space.

-1

Previously it was MemFree + catched ( which is correct many years back).

Now it is MemFree + Active(file) + Inactive(file) + SRreclaimable .

For further reference go trough the link below by LINUX TORVALD.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.