0

I recently had an issue where a rogue process caused some ec2 instances to run out of memory and crash. It probably shouldn't have been an issue, except I was using t2.small instances which are pretty memory constrained and it ran out of memory before someone could respond to our monitoring alerts.

Obviously it's best to avoid using swap if possible, but if I had swap enabled my instances probably would have stayed online albeit with degraded performance.

Given that hard disk space is usually abundant it seems like a good idea to enable swap even if it's only required in exceptional circumstances.

By default OpsWorks only enables swap for micro instances, not small instances. Why would they be so conservative in enabling swap, is there a reason I might not want to enable swap if I don't need it for normal operation?

thexacre
  • 1,849
  • 12
  • 14

3 Answers3

4

Did you run out of physical memory or did you run out of backing store? If the latter, swap would have let your system continue to operate normally even without any data being written to it!

"[I]s there a reason I might not want to enable swap if I don't need it for normal operation?"

Yes. Having swap available is necessary for the OS to make efficient use of physical RAM.

For example, suppose a process creates a private, writable mapping of a 256MB file. If there's no swap, the OS has to reserve 256MB of physical RAM for this mapping, even if none of that RAM is ever needed because the mapping is never modified just in case the process decides to modify every byte of that mapping.

Essentially, without swap, every clean writable page not backed by a file means one page of physical RAM that cannot hold modified data, even if those pages remain discardable for their entire lifetime.

You really, really want swap available even if you don't need it.

David Schwartz
  • 31,215
  • 2
  • 53
  • 82
  • 2
    Thanks for the answer, although this is mostly an answer to why I would want swap rather than why I might not. It does make me wonder even more why the default settings would be so conservative with regard to swap though. – thexacre Jan 06 '15 at 01:47
1

I think the key is that you need to know what memory your process is going to need. If you're using EC2 that means you've picked an instance size that will cover some workload; often I've seen that workload is only one process/service. Adding a bunch of swap is cheap but you need to know when/why it's using that swap. Too often I see answers here that say; "Add swap in case some inactive processes are eating up a bit of RAM", that makes sense for desktop but on your EC2 instance, you should know the running characteristics of your process(es) -- this is especially the case when you're developing an application -- know how it typically runs by disabling swap during development then add swap and increase swappiness to a high value (90+) when in production.

Another way of looking at it is that in a server environment and especially in the cloud where you can tailor your instance to your workload do you really want your process swapping? Is that OK for performance? You're not going to be a happy camper when you get a one-size-too-small 2GB RAM machine that runs a process that needed 3GB to run comfortably continuously swapping pages onto disk.

c4urself
  • 5,270
  • 3
  • 25
  • 39
0

YMMV, but for interactive workloads, my experience has been that if you ever start to use the page file, overall system performance grinds to a halt anyway, and it's usually caused by some runaway process, so I'd much rather have that program crash (out of memory) than kill the performance of all my other programs. So, I configured my home shared family machine with no page file and loads of extra RAM. Complaints from my wife about bad performance went down significantly. When I later added an SSD to the mix last year, things got even better.

James
  • 363
  • 2
  • 4
  • 16