# Figuring out what's eating all my memory



## Anon (Dec 4, 2011)

Running FBSD FreeBSD 8.2 as a server, something is unusual about my memory consumption. I'm a newbie so I'm just wondering how I would go about figuring out what's eating up my memory?


----------



## SNK (Dec 4, 2011)

What do you mean by unusual? Can you post a summary of your memory usage?


----------



## Anon (Dec 4, 2011)

SNK said:
			
		

> What do you mean by unusual? Can you post a summary of your memory usage?



Sure. I usually have 1800Mbs+ of RAM free, my server isn't receiving any more visitors than normal, so I can't figure out why I've lost so much free RAM in the span of a month.


----------



## Christofer (Dec 4, 2011)

Tweak mysql, nginx/php. and the network maybe helps.


----------



## gkontos (Dec 4, 2011)

Anon said:
			
		

> Sure. I usually have 1800Mbs+ of RAM free, my server isn't receiving any more visitors than normal, so I can't figure out why I've lost so much free RAM in the span of a month.



Unused memory is wasted memory! Mysql is caching a lot, this is not bad. But if you still continue to have so much free memory and you don't anticipate a significant increase in traffic, then I suggest that you provide more memory to mysql.

Regards,
George


----------



## vertexSymphony (Dec 5, 2011)

I don't see anything unusual in there (at first sight) ... as other one said â†’ unused memory is wasted memory
What is not being used by an application is usually used by filesystem cache, and that memory can be freed if a process needs it ... otherwise, it's good to avoid wasting it and use it in caching in stuff that you access a lot, so the system performs better.

Regards, Alex.

P.S â†’ Sorry for my terrible english


----------



## fluca1978 (Dec 5, 2011)

If I get it right you have still 1.2 GB of available free ram and the only process that seems to consume a lot of memory alone is mysqld. Now, databases have to do that, since memory is faster than disk I/O, so they tend to keep in memory as much data they can (and this is a reason for doing a good tuning against fsync behaviors and to have transactions) to speed up data access. Moreover, memory consumption depends on how many clients and how many data your server has to serve. You do not want to go to disk for each single tuple...


----------



## peetaur (Dec 8, 2011)

For more details about memory in top, sort by "RES". Hit o and then "res" and enter.

And show us the output of `# netstat -m`.

If those numbers are normal, then nothing seems wrong.

And there is nothing at all wrong with high amounts of "Wired" and "Cache". Your system uses those when you load files, such as when you run commands and it loads the shared libraries into memory, so the next run of the same command is fast. For example, here is my system with 48 GB of memory, and not much running. I expect lots of wired, because ZFS likes to cache a whole lot. I would be unhappy if my 48 GB was unused, going to waste.


```
CPU:  0.0% user,  0.0% nice,  0.0% system,  0.0% interrupt,  100% idle
Mem: 4788K Active, 447M Inact, 43G Wired, 4236K Cache, 48K Buf, 3340M Free
Swap: 1024M Total, 1024M Free

  PID USERNAME   THR PRI NICE   SIZE    RES STATE   C   TIME   WCPU COMMAND
36162 peter        1  44    0 29892K  3036K select  3   0:00  0.00% sshd
35741 root         1  44    0 29892K  2772K sbwait  8   0:00  0.00% sshd
96142 root         1  44    0 10248K  2192K wait    8   0:00  0.00% bash
  163 root         1  44    0 50504K  1916K select  8   3:29  0.00% smbd
84344 root         1  44    0  9372K  1908K CPU8    8   0:00  0.00% top
36397 peter        1  44    0 10248K  1896K wait    8   0:00  0.00% bash
95344 root         1  44    0 10312K  1508K pause  11   0:00  0.00% csh
59705 root         1  44    0 49216K  1328K select  0   0:00  0.00% smbd
95290 peter        1  44    0 21692K  1280K wait    1   0:00  0.00% su
83292 peter        1  44    0 29892K  1268K select  8   0:03  0.00% sshd
58601 root         1  44    0 38200K  1256K select  9   0:21  0.00% nmbd
87490 root         1  44    0 10248K  1244K ttyin   2   0:00  0.00% bash
37175 root         1  44    0 14528K  1192K nanslp  8   0:18  0.00% gstat
62832 root         1  44    0 49216K  1180K select  0   0:00  0.00% smbd
87400 root         1  44    0  9372K  1180K select  9   0:06  0.00% screen
```

Here is an example of caching:

```
# dd if=/tank/images/openSUSE-11.4-DVD-x86_64.iso of=/dev/null bs=128k
35208+0 records in
35208+0 records out
4614782976 bytes transferred in 0.977634 secs (4720358741 bytes/sec)
```

4 GBin under a second = 4.7 GB per second. Not bad, eh? Well, it was cached. I ran md5 on it just before the above dd.


----------



## peetaur (Dec 8, 2011)

And another relevant thing (which I didn't know and had to look up):

Post by Beastie http://forums.freebsd.org/showthread.php?t=8591


> Free memory is memory that has *never* been used.
> In inactive memory you may have clean or dirty pages. Dirty pages are no longer in use, may be removed in the near future (after being synced to disk) and reallocated for something else.



So add up your "Free" and "Inact" from now on when considering how much memory you have "free for allocation".


----------

