Subject: Question about free/used memory on Linux

Hi kernel gurus,
I am trying to find out the memory that's used on my linux box.
I find that there are quite a few confusing metrics. How do
I find out the "true" used memory ?

1. For eg. "free -m" shows free memory (excluding buffers/caches)
as 308 MB while I can see(from "df" output) that the the tmpfs
partitions take up about 400 MB. So, does "free -m" not consider
the tmpfs partitions ?

2. I try to add up RSS field of all processes reported by
"ps aux" command. But is it true that this would be misleading
in that, shared memory used by, say 2 processes would show
up twice here although there's only one copy in memory. Also
does this consider the fact that there's only one copy
of shared libraries ?

3. I guess "free -m" and "top" commands use /proc/meminfo
and hence all these outputs are same ?

Thanks,
Ravi


2007-10-21 18:53:53

by Vaidyanathan Srinivasan

[permalink] [raw]
Subject: Re: Question about free/used memory on Linux

Ravinandan Arakali (rarakali) wrote:
> Hi kernel gurus,
> I am trying to find out the memory that's used on my linux box.
> I find that there are quite a few confusing metrics. How do
> I find out the "true" used memory ?

pagecache pages may be the reason for the confusion. Most free memory can
be consumed under 'Cached' in /proc/meminfo

Most of this memory is easily reclaimable and can be considered 'free'.
However the cost of reclaim increases if the pages are memory mapped and/or
dirty.

Cached-Mapped-Dirty in /proc/meminfo can be seen as a easily freeable
memory and can be added to the 'Free' in /proc/meminfo count

What is your intention of finding free memory in the system? Linux kernel
takes the best decision of using available RAM for file cache or
applications code/data as and when needed. Ideally the 'Free' count will
be a very low value on a heavily used system. If application needs more
memory, then the kernel will shrink the caches and give the reclaimed
memory to the application.

>
> 1. For eg. "free -m" shows free memory (excluding buffers/caches)
> as 308 MB while I can see(from "df" output) that the the tmpfs
> partitions take up about 400 MB. So, does "free -m" not consider
> the tmpfs partitions ?

Pages used from tmpfs should come under Mapped or Cached. They are not
counted as free.

> 2. I try to add up RSS field of all processes reported by
> "ps aux" command. But is it true that this would be misleading
> in that, shared memory used by, say 2 processes would show
> up twice here although there's only one copy in memory. Also
> does this consider the fact that there's only one copy
> of shared libraries ?
>

RSS is from each process point of view. If the page is present in RAM, it
is counted. If the pages is shared, then it is counted in both process
address space.

> 3. I guess "free -m" and "top" commands use /proc/meminfo
> and hence all these outputs are same ?

Yes, all of them parse /proc/meminfo

--Vaidy

Subject: RE: Question about free/used memory on Linux

Hi Vaidy,
Thanks for clarifying several of my doubts.

To answer your question about my intention, we currently have a
system with 2 GB RAM and I need to find out the actual used and
free memory so that we can decide if the same setup(applications,
tmpfs etc.) can run on another system with lesser memory.

Is it correct to say that the "used" field "free -m" excluding
buffers/caches would give the correct idea of used memory
(I mean does it take care of shared memory, shared copies of
libraries etc.) ? I assume it does not include /dev/shm usage
since that's also a tmpfs partition ?

If so, then I can add the memory used by tmpfs partitions to
the above and get the total memory used ?

For eg. if my "free -m" appears as below:
Linux(debug)# free -m
total used free shared buffers
cached
Mem: 2014 984 1030 0 80
594
-/+ buffers/cache: 309 1705

Can I say that 309MB + 350 MB(size of tmpfs partitions including
/dev/shm)
is the used memory on my system ?

Thanks,
Ravi


-----Original Message-----
From: Vaidyanathan Srinivasan [mailto:[email protected]]
Sent: Sunday, October 21, 2007 12:01 PM
To: Ravinandan Arakali (rarakali)
Cc: Linux Kernel
Subject: Re: Question about free/used memory on Linux

Ravinandan Arakali (rarakali) wrote:
> Hi kernel gurus,
> I am trying to find out the memory that's used on my linux box.
> I find that there are quite a few confusing metrics. How do I find out

> the "true" used memory ?

pagecache pages may be the reason for the confusion. Most free memory
can be consumed under 'Cached' in /proc/meminfo

Most of this memory is easily reclaimable and can be considered 'free'.
However the cost of reclaim increases if the pages are memory mapped
and/or dirty.

Cached-Mapped-Dirty in /proc/meminfo can be seen as a easily freeable
memory and can be added to the 'Free' in /proc/meminfo count

What is your intention of finding free memory in the system? Linux
kernel takes the best decision of using available RAM for file cache or
applications code/data as and when needed. Ideally the 'Free' count
will be a very low value on a heavily used system. If application
needs more memory, then the kernel will shrink the caches and give the
reclaimed memory to the application.

>
> 1. For eg. "free -m" shows free memory (excluding buffers/caches) as
> 308 MB while I can see(from "df" output) that the the tmpfs partitions

> take up about 400 MB. So, does "free -m" not consider the tmpfs
> partitions ?

Pages used from tmpfs should come under Mapped or Cached. They are not
counted as free.

> 2. I try to add up RSS field of all processes reported by "ps aux"
> command. But is it true that this would be misleading in that, shared
> memory used by, say 2 processes would show up twice here although
> there's only one copy in memory. Also does this consider the fact that

> there's only one copy of shared libraries ?
>

RSS is from each process point of view. If the page is present in RAM,
it is counted. If the pages is shared, then it is counted in both
process address space.

> 3. I guess "free -m" and "top" commands use /proc/meminfo and hence
> all these outputs are same ?

Yes, all of them parse /proc/meminfo

--Vaidy

2007-10-22 05:44:07

by Vaidyanathan Srinivasan

[permalink] [raw]
Subject: Re: Question about free/used memory on Linux


Ravinandan Arakali (rarakali) wrote:
> Hi Vaidy,
> Thanks for clarifying several of my doubts.
>
> To answer your question about my intention, we currently have a
> system with 2 GB RAM and I need to find out the actual used and
> free memory so that we can decide if the same setup(applications,
> tmpfs etc.) can run on another system with lesser memory.
>
> Is it correct to say that the "used" field "free -m" excluding
> buffers/caches would give the correct idea of used memory
> (I mean does it take care of shared memory, shared copies of
> libraries etc.) ? I assume it does not include /dev/shm usage
> since that's also a tmpfs partition ?

Thats correct. The used excluding the buffer caches gives most of the
memory used by the system. You have excludes _all_ file backed memory
including shm.

>
> If so, then I can add the memory used by tmpfs partitions to
> the above and get the total memory used ?
>
> For eg. if my "free -m" appears as below:
> Linux(debug)# free -m
> total used free shared buffers
> cached
> Mem: 2014 984 1030 0 80
> 594
> -/+ buffers/cache: 309 1705
>
> Can I say that 309MB + 350 MB(size of tmpfs partitions including
> /dev/shm)
> is the used memory on my system ?

Two problems with this logic:

1. all of tmpfs may not be really used. You are over committing.
2. You still miss the pages needed to map the program code. They are file
backed too. Though this will be very less amount of memory compared to data
and shared memory.

Let me suggest a metric:

1. Take the used part excluding the buffers (309MB) in your case and add
'Mapped' and 'Dirty' from /proc/meminfo

This may be better than adding tmpfs/shmdev size.

2. Once your system is running will all applications loaded, cleanup the
pagecache (file data cached in memory)

sync
echo 1 > /proc/sys/vm/drop_caches

The first sync will bring down 'Dirty' count and drop_caches will reclaim
all 'not needed' file cache memory.

Now if you use 'free' and take the used count _with_ the buffers and file
cache, this will provide a realistic value. (Actually Free in /proc/meminfo)

Do not exclude buffers they are _needed_ for optimum system operation.
With the above figure you can probably add 10% or more memory as extra
memory for file cache when the system is operating with full load.

If you want to be sure of these experiments boot you system with less
memory using mem=xxx kernel parameter and run some performance tests to
ensure the degradation is under acceptable limits.

--Vaidy


Subject: RE: Question about free/used memory on Linux

Hi Vaidy,
I will use the metric you have suggested.

Thanks,
Ravi


-----Original Message-----
From: Vaidyanathan Srinivasan [mailto:[email protected]]
Sent: Sunday, October 21, 2007 10:50 PM
To: Ravinandan Arakali (rarakali)
Cc: Linux Kernel
Subject: Re: Question about free/used memory on Linux


Ravinandan Arakali (rarakali) wrote:
> Hi Vaidy,
> Thanks for clarifying several of my doubts.
>
> To answer your question about my intention, we currently have a system

> with 2 GB RAM and I need to find out the actual used and free memory
> so that we can decide if the same setup(applications, tmpfs etc.) can
> run on another system with lesser memory.
>
> Is it correct to say that the "used" field "free -m" excluding
> buffers/caches would give the correct idea of used memory (I mean does

> it take care of shared memory, shared copies of libraries etc.) ? I
> assume it does not include /dev/shm usage since that's also a tmpfs
> partition ?

Thats correct. The used excluding the buffer caches gives most of the
memory used by the system. You have excludes _all_ file backed memory
including shm.

>
> If so, then I can add the memory used by tmpfs partitions to the above

> and get the total memory used ?
>
> For eg. if my "free -m" appears as below:
> Linux(debug)# free -m
> total used free shared buffers
> cached
> Mem: 2014 984 1030 0 80
> 594
> -/+ buffers/cache: 309 1705
>
> Can I say that 309MB + 350 MB(size of tmpfs partitions including
> /dev/shm)
> is the used memory on my system ?

Two problems with this logic:

1. all of tmpfs may not be really used. You are over committing.
2. You still miss the pages needed to map the program code. They are
file backed too. Though this will be very less amount of memory compared
to data and shared memory.

Let me suggest a metric:

1. Take the used part excluding the buffers (309MB) in your case and add
'Mapped' and 'Dirty' from /proc/meminfo

This may be better than adding tmpfs/shmdev size.

2. Once your system is running will all applications loaded, cleanup the
pagecache (file data cached in memory)

sync
echo 1 > /proc/sys/vm/drop_caches

The first sync will bring down 'Dirty' count and drop_caches will
reclaim all 'not needed' file cache memory.

Now if you use 'free' and take the used count _with_ the buffers and
file cache, this will provide a realistic value. (Actually Free in
/proc/meminfo)

Do not exclude buffers they are _needed_ for optimum system operation.
With the above figure you can probably add 10% or more memory as extra
memory for file cache when the system is operating with full load.

If you want to be sure of these experiments boot you system with less
memory using mem=xxx kernel parameter and run some performance tests to
ensure the degradation is under acceptable limits.

--Vaidy

2007-10-22 10:06:18

by Pádraig Brady

[permalink] [raw]
Subject: Re: Question about free/used memory on Linux

Ravinandan Arakali (rarakali) wrote:
> Hi kernel gurus,
> I am trying to find out the memory that's used on my linux box.
> I find that there are quite a few confusing metrics. How do
> I find out the "true" used memory ?
>
> 1. For eg. "free -m" shows free memory (excluding buffers/caches)
> as 308 MB while I can see(from "df" output) that the the tmpfs
> partitions take up about 400 MB. So, does "free -m" not consider
> the tmpfs partitions ?
>
> 2. I try to add up RSS field of all processes reported by
> "ps aux" command. But is it true that this would be misleading
> in that, shared memory used by, say 2 processes would show
> up twice here although there's only one copy in memory. Also
> does this consider the fact that there's only one copy
> of shared libraries ?

Have a look at this script so show RAM used by programs:
http://www.pixelbeat.org/scripts/ps_mem.py

Note to display totals you will need this patch applied:
http://lkml.org/lkml/2007/8/13/1224

P?draig.

2007-10-22 11:54:54

by Helge Hafting

[permalink] [raw]
Subject: Re: Question about free/used memory on Linux

Ravinandan Arakali (rarakali) wrote:
> Hi Vaidy,
> Thanks for clarifying several of my doubts.
>
> To answer your question about my intention, we currently have a
> system with 2 GB RAM and I need to find out the actual used and
> free memory so that we can decide if the same setup(applications,
> tmpfs etc.) can run on another system with lesser memory.
>
You can test this directly.
Boot your 2G machine with
mem=<size of smaller system>
Then the machine uses less memory, you get
a realistic test of how the smaller machine will
perform.


Helge Hafting

2007-10-22 17:34:18

by linux-os (Dick Johnson)

[permalink] [raw]
Subject: RE: Question about free/used memory on Linux



On Sun, 21 Oct 2007, Ravinandan Arakali (rarakali) wrote:

> Hi Vaidy,
> Thanks for clarifying several of my doubts.
>
> To answer your question about my intention, we currently have a
> system with 2 GB RAM and I need to find out the actual used and
> free memory so that we can decide if the same setup(applications,
> tmpfs etc.) can run on another system with lesser memory.
>
> Is it correct to say that the "used" field "free -m" excluding
> buffers/caches would give the correct idea of used memory
> (I mean does it take care of shared memory, shared copies of
> libraries etc.) ? I assume it does not include /dev/shm usage
> since that's also a tmpfs partition ?
>
> If so, then I can add the memory used by tmpfs partitions to
> the above and get the total memory used ?
>
> For eg. if my "free -m" appears as below:
> Linux(debug)# free -m
> total used free shared buffers
> cached
> Mem: 2014 984 1030 0 80
> 594
> -/+ buffers/cache: 309 1705
>
> Can I say that 309MB + 350 MB(size of tmpfs partitions including
> /dev/shm)
> is the used memory on my system ?
>
> Thanks,
> Ravi
>
[Snipped...]

Just boot your present system with MEM=nnn on the command-line,
where nnn is the amount of memory you would like to use in the
new system (like this).

title Fedora Core (2.6.16.24)
root (hd0,0)
kernel /boot/vmlinuz-2.6.16.24 ro root=LABEL=/ mem=768m nmi_watchdog=0
rhgb reboot=cb
initrd /boot/initrd-2.6.16.24.img



Cheers,
Dick Johnson
Penguin : Linux version 2.6.16.24 on an i686 machine (5592.59 BogoMips).
My book : http://www.AbominableFirebug.com/
_


****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

Subject: RE: Question about free/used memory on Linux

Thanks all for the answers.

I could load by specifying lower amount of memory but on the newer
platform(does not exist), some of the existing components will
not exist while some newer ones will be added. So, it won't be
entirely accurate but I could still get some idea by trying
it out.

Thanks,
Ravi

-----Original Message-----
From: P?draig Brady [mailto:[email protected]]
Sent: Monday, October 22, 2007 3:05 AM
To: Ravinandan Arakali (rarakali)
Cc: Linux Kernel
Subject: Re: Question about free/used memory on Linux

Ravinandan Arakali (rarakali) wrote:
> Hi kernel gurus,
> I am trying to find out the memory that's used on my linux box.
> I find that there are quite a few confusing metrics. How do I find out
> the "true" used memory ?
>
> 1. For eg. "free -m" shows free memory (excluding buffers/caches) as
> 308 MB while I can see(from "df" output) that the the tmpfs partitions
> take up about 400 MB. So, does "free -m" not consider the tmpfs
> partitions ?
>
> 2. I try to add up RSS field of all processes reported by "ps aux"
> command. But is it true that this would be misleading in that, shared
> memory used by, say 2 processes would show up twice here although
> there's only one copy in memory. Also does this consider the fact that
> there's only one copy of shared libraries ?

Have a look at this script so show RAM used by programs:
http://www.pixelbeat.org/scripts/ps_mem.py

Note to display totals you will need this patch applied:
http://lkml.org/lkml/2007/8/13/1224

P?draig.

Subject: RE: Question about free/used memory on Linux

Hi Vaidy,
What do you think is the right way to get the memory usage of a
process, I mean the actual physical memory used ? Basically,
I'm interested in the incremental cost of a process, which
means, I don't want to include the text segments of shared
libraries which would remain even after the process is killed
(since it would be used by other processes).

Is the RSS field of "ps aux" command the right one or use
"pmap" command and look at the "writeable" segments ?

Thanks,
Ravi

-----Original Message-----
From: Vaidyanathan Srinivasan [mailto:[email protected]]
Sent: Sunday, October 21, 2007 10:50 PM
To: Ravinandan Arakali (rarakali)
Cc: Linux Kernel
Subject: Re: Question about free/used memory on Linux


Ravinandan Arakali (rarakali) wrote:
> Hi Vaidy,
> Thanks for clarifying several of my doubts.
>
> To answer your question about my intention, we currently have a system

> with 2 GB RAM and I need to find out the actual used and free memory
> so that we can decide if the same setup(applications, tmpfs etc.) can
> run on another system with lesser memory.
>
> Is it correct to say that the "used" field "free -m" excluding
> buffers/caches would give the correct idea of used memory (I mean does

> it take care of shared memory, shared copies of libraries etc.) ? I
> assume it does not include /dev/shm usage since that's also a tmpfs
> partition ?

Thats correct. The used excluding the buffer caches gives most of the
memory used by the system. You have excludes _all_ file backed memory
including shm.

>
> If so, then I can add the memory used by tmpfs partitions to the above

> and get the total memory used ?
>
> For eg. if my "free -m" appears as below:
> Linux(debug)# free -m
> total used free shared buffers
> cached
> Mem: 2014 984 1030 0 80
> 594
> -/+ buffers/cache: 309 1705
>
> Can I say that 309MB + 350 MB(size of tmpfs partitions including
> /dev/shm)
> is the used memory on my system ?

Two problems with this logic:

1. all of tmpfs may not be really used. You are over committing.
2. You still miss the pages needed to map the program code. They are
file backed too. Though this will be very less amount of memory compared
to data and shared memory.

Let me suggest a metric:

1. Take the used part excluding the buffers (309MB) in your case and add
'Mapped' and 'Dirty' from /proc/meminfo

This may be better than adding tmpfs/shmdev size.

2. Once your system is running will all applications loaded, cleanup the
pagecache (file data cached in memory)

sync
echo 1 > /proc/sys/vm/drop_caches

The first sync will bring down 'Dirty' count and drop_caches will
reclaim all 'not needed' file cache memory.

Now if you use 'free' and take the used count _with_ the buffers and
file cache, this will provide a realistic value. (Actually Free in
/proc/meminfo)

Do not exclude buffers they are _needed_ for optimum system operation.
With the above figure you can probably add 10% or more memory as extra
memory for file cache when the system is operating with full load.

If you want to be sure of these experiments boot you system with less
memory using mem=xxx kernel parameter and run some performance tests to
ensure the degradation is under acceptable limits.

--Vaidy

2007-11-14 11:58:30

by Pádraig Brady

[permalink] [raw]
Subject: Re: Question about free/used memory on Linux

Ravinandan Arakali (rarakali) wrote:
> Hi Vaidy,
> What do you think is the right way to get the memory usage of a
> process, I mean the actual physical memory used ? Basically,
> I'm interested in the incremental cost of a process, which
> means, I don't want to include the text segments of shared
> libraries which would remain even after the process is killed
> (since it would be used by other processes).
>
> Is the RSS field of "ps aux" command the right one or use
> "pmap" command and look at the "writeable" segments ?

I already commented on this thread with a python script
for reporting RAM usage for programs.

RSS = Private and Shared Resident pages.
You can get the shared value for a process from /proc/$$/smaps
(as is done in the script)

P?draig.

Subject: RE: Question about free/used memory on Linux

Thanks Padraig.

Any idea how to get the size of running linux kernel ?

Ravi

-----Original Message-----
From: P?draig Brady [mailto:[email protected]]
Sent: Wednesday, November 14, 2007 3:58 AM
To: Ravinandan Arakali (rarakali)
Cc: Vaidyanathan Srinivasan; Linux Kernel
Subject: Re: Question about free/used memory on Linux

Ravinandan Arakali (rarakali) wrote:
> Hi Vaidy,
> What do you think is the right way to get the memory usage of a
> process, I mean the actual physical memory used ? Basically, I'm
> interested in the incremental cost of a process, which means, I don't
> want to include the text segments of shared libraries which would
> remain even after the process is killed (since it would be used by
> other processes).
>
> Is the RSS field of "ps aux" command the right one or use "pmap"
> command and look at the "writeable" segments ?

I already commented on this thread with a python script for reporting RAM usage for programs.

RSS = Private and Shared Resident pages.
You can get the shared value for a process from /proc/$$/smaps (as is done in the script)

P?draig.