0

I have a laptop with 8GB of RAM running 4.0.4-2-ARCH. Recently, I installed Android Studio, and bam, my normally pristine system was suddenly stuttering and all out freezing (more than once).

Before Android Studio, I comfortably ran SMB, Minidlna, Plex, MySQL, PostgreSQL, Apache and Chrome simultaneously with no issues. Now I struggled to run even Chrome with Android Studio. In both cases, both free and System Monitor reported only 6.5G used RAM!

So I did a little digging and enabled a swap of 5G (swapfile). I was surprised by the performance improvement! No more lags. But still, during peak load, (Studio and Chrome) the usage was 5G RAM + 1.5G swap.

This confused me a bit and I have two questions.

Firstly, if used memory was only 6G, why the stutter and especially, why the freezes?

Secondly, my hard drive (1TB) is about 3 years old, and I would rather keep swap disabled. Is there some other way to achieve swap-like performance, without stressing the hard drive. I have already set swappiness to a low value of 10, which uses about 1G normally, and my laptop is already at it's maximum RAM limit.

I have already read these excellent answers, but I am asking this because I am not satisfied by them.

EDIT: These answers state that Linux will use all available memory and hence, for new programs, paging will slow down the computer. But if a lot the content Linux put into memory is stuff that can be managed without having done so, why is performance penalized when programs requiring large RAM are run? I mean, at best program boot up should be slow (paging).

  1. Do I need swap space if I have more than enough amount of RAM?

  2. Why is swap used when a lot of memory is still free?

  3. Is swap an anachronism?

xyz
  • 103

1 Answers1

1

While it may appear you have more than enough RAM, Linux buffers file data in memory. It is also common to place file systems like /tmp in memory to speed up access. If you don't have swap enabled, there are a lot of things that may end up stuck in memory that may prevent caching of frequently accessed files. Your choice is really: page out unused memory to disk; or repeatedly read files from disk. You have no options that won't require IO once memory (including buffers) gets filled.

These days it is common to page memory out to swap that hasn't been recently accessed rather than swap out whole programs. Things that might be paged include:

  • Non-PIC (position-independent-code) that has been loaded into a program and modified for its memory location;
  • Data read into programs that is not being actively used;
  • temporary files that are not being actively used (may be paged); and
  • any other memory that isn't being actively used and has no alternate backing store.

PIC and other unmodified data read from disk may use the file from which it is read as a backing store rather than using swap.

You can use a program like sar to monitor paging, swapping, and disk I/O. I would expect you will see less use of disk when you have swap enabled.

If you want to suspend to disk, it is common that you need a fairly large swap space into which memory can be copied when you suspend.

BillThor
  • 8,965
  • Ok, it makes sense to cache files to speed up access and using RAM effectively, but like you said, even if Linux simply dumps non-frequently used pages (eg, a file I opened earlier, and only once), it shouldn't impact performance. New programs should not be memory constrained because of old cached files – xyz Jun 01 '15 at 05:16
  • Can you please clarify on "see less use of disk when you have swap enabled". Even if a file is cached to swap, it's still on disk right? – xyz Jun 01 '15 at 05:17
  • @prakharsingh95 Things paged out to disk tend not to be frequently accessed. If they are using memory that would otherwise be used for frequently accessed, they may increase the I/O load (use of disk). This is trading off space used for swap for performance resulting from fewer waits for disk I/O. – BillThor Jun 01 '15 at 16:51
  • I guess that no swap prevented/slowed catching of files, and since studio generates lots of files, this issue became relevant. Thanks, I'll keep swap enabled. – xyz Jun 01 '15 at 16:54