1

I'm trying to confirm that there is a memory leak on my Linux device while in idle state (there should be no continuous memory consumption). I'm not interested in identifying the process that is causing the leak but just to conclude the memory is leaking.

I'm using top as the diagnostic tool. Example of memory fields from top:

Mem:    118616k total,    83980k used,    34636k free,        0k buffers
Swap:        0k total,        0k used,        0k free,    24140k cached

I can see the value of free continuously dropping, however, the value of cached constantly increasing.

To confirm that there is a memory leak, the sum free+cached should be continuously dropping as opposed to just free decreasing (in which case the kernel could just be using free memory for caching)?

  • As far as I know when *the sum `free+cached` is continuously dropping* the only valid objective conclusion is that there is more memory in use. By itself that is insufficient data to conclude a memory leak. – HBruijn Apr 07 '15 at 13:43
  • Forgot to mention that the system is in idle state, will update – TheMeaningfulEngineer Apr 07 '15 at 13:44

2 Answers2

0

There's non way to know if you have a memory leak. You can suppose it, based on your knowledge of the system and programs instead it. I'd check the file /proc/meminfo, taking the value of MemFree and checking it in some intervals of time. If it is always decreasing, after some intervals, I should assert there is a memory leak.

  • cat /proc/meminfo | gawk '{if (/MemFree/ ~ $0) print $2}'

But as I know, there's no way to check a "global" memory leak. Wait for other answers ;)

Echoes_86
  • 173
  • 10
  • Updated the question. My aim is to try and confirm that if there is a continuous memory leak caused by a userspace process, it isn't enough to just observe `free` because the decrease there might just mean the kernel is using the memory for caching. – TheMeaningfulEngineer Apr 07 '15 at 14:17
0

Memory leak of the userspace process would result in the constantly growing amount of memory used by this process.

You could just monitor the processes using top in batch mode (however, it seems that top does not support sorting by memory in the batch mode, https://bugzilla.redhat.com/show_bug.cgi?id=547749)

As alternative to top, you could also execute the following command every minute:

ps aux --sort -rss | head -n 10

to see top 10 processes using RAM.

E.g.

while [ true ]; do ps aux --sort -rss | head -n 10 >> memory_usage_log.txt; echo "#########" >> memory_usage_log.txt; sleep 60; done;

If some process uses more and more RAM with time, while there is no activity or input data, then it could be a memory leak (e.g., if you start a web browser and keeps it running without using it, the memory usage of the web browser obviously should not grow up with time).

Andrey Sapegin
  • 1,201
  • 2
  • 12
  • 27