2

Here is a quick intro to the problem, but if you're interested only on the question, I think you may skip to the last paragraph.

I have installed Ubuntu Mate on my laptop with the recommended amount of swap, (according this answer and this page and soem other things I read). According to my uses I figured that that should be enough (I have 5GB of ram so I set up 5GB of swap). However, whenever I check my swap space, I notice it is empty. Similarly, it is very hard for me to see my RAM with more than 50% use. This is making me think that I may have oversized my swap space (I don't need hibernation to be always possible btw). For me the only way to be sure is monitoring how much memory/swap I use on a daily basis.

So the question is: how do I monitor the swap/memory use on my system? (So I can open a file at the end of the week and be able to plot my memory and swap usage over the last days.) If that already exists (I haven't yet found anything), how do I access it?

PS.: I would rather avoid writing a script that runs every x seconds on crontab and collect this information.

4

You only use swap when:

  • You're out of RAM. Inactive things in RAM are paged out to disk (in swap).
  • You hibernate. The entire contents of RAM is dumped in swap so it can be loaded when the power is back on.

Having some swap is very useful but you only need a big swap if you want to hibernate. Otherwise a couple of gigabytes as a reserve is good enough.

And 5GB is a really weird number. RAM usually works fastest in equally specced pairs or triplets (depending on your system). You might get better performance from 2×2GB than 1×2GB+3×1GB (even though the second is more RAM).

But for long-term logging, there are a number of options. sysstat looks like the easiest deploy-and-forget.

2

The vmstat command provides all the numerical data you need (and probably more). You can let it run in the background and write its statistics to a file.

  • note that you need to provide a delay or vmstat will only produce one line of data. a delay without a count parameter it will produce output indefinitely (eventually filling your storage space. See man vmstat – Elder Geek Feb 29 '16 at 4:18
1

No script is required.

The math to do this with vmstat is fairly straightforward.

vmstat X count where X is the delay in seconds and count is the number of stat reports (lines). so for 7 days (end of the week) so if the delay was 5 seconds you'd have 5x12(minute)x60(hour)x24(day)x7(week) resulting in a count of 604800x about 80 chars per line will result in a log file of approximately 48,384,000 bytes in size.

The command to do that is vmstat 5 604800 > swap.log Of course this means the machine is running 24/7. If the schedule is different or you prefer a smaller log file adjust the count and delay according to your requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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