540eacf99a4dc3396fba59093306ccb483ff74c6
[webi-installers/.git] / vps-addswap / README.md
1 ---
2 title: VPS Add Swap
3 homepage: https://webinstall.dev/vps-addswap
4 tagline: |
5   VPS Add Swap: because a little RAM can go a long way.
6 linux: true
7 description: |
8   Creates permanent swap space that will be activated on each boot.
9
10   `vps-addswap` will
11
12     1. create a permanent swapfile `/var/swapfile`
13     2. format and activate the swapfile
14     3. add '/var/swapfile none swap sw 0 0' to `/etc/fstab`
15 ---
16
17 ### What is `swap`?
18
19 Simply put, swap space is a substitute for RAM that lives on a storage drive.
20
21 ### Why use permanent `swap`?
22
23 In a word: Money.
24
25 Whereas once upon a time RAM was expensive in real life, now it is very cheap.
26
27 In "the cloud", however, RAM is not just expensive - it's very, _very_
28 expensive - as in _hundreds_-of-dollars-per-month expensive.
29
30 For most applications this isn't a problem because you don't need much RAM - or
31 when you do, you need it according to workload, which is easy (and cheap) to
32 distribute across many otherwise underpowered server instances.
33
34 However, when you need to use Python, R, etc to process large batches of data
35 sequentially - such as the multi-gigabyte datasets common in Machine Learning,
36 Artificial Intelligience, Data Mining, etc - or when you need a very rare burst
37 of RAM for a very short-lived task - such as an `npm run bild`, swap can be a
38 big cost saver.
39
40 The good news is that quite often you can also have your cake and eat it to:
41 most language runtimes (i.e. Node.js, Go, Java, etc) allow you to tune
42 parameters that limit the amount of RAM they are allowed to allocate
43 dynamically, which means that you can take advantage of large swap space for
44 sequential processing tasks without causing your apps to be slow.
45
46 ### How to allocate swap space?
47
48 Typically you should place swap files in `/var`, which is the volume that will
49 be optimized for fast writes (on servers that do so).
50
51 ```bash
52 sudo fallocate -l 2G /var/swapfile
53 sudo chmod 0600 /var/swapfile
54 sudo mkswap /var/swapfile
55 ```
56
57 This method is preferrably to `truncate` and `dd` for SSDs as it will NOT
58 actually write the file to its full size, and therefore will be instant.
59
60 On an HDD (rotational drive), `dd` may be a better choice, as you need to
61 allocate contiguous space all at once.
62
63 ```bash
64 sudo dd if=/dev/zero of=/var/swapfile bs=2G count=1
65 ```
66
67 ### How much swap to use?
68
69 If you need to run common tasks with `npm run build` - such as compiling
70 `sass` - 2G would be a good place to start.
71
72 If you need to process large datasets, somewhere between 1x-2x the size of the
73 largest dataset should do - noting that the dataset will likely take up more
74 space in RAM/swap than as a file on disk.
75
76 ### How to activate swap space?
77
78 ```bash
79 sudo swapon /var/swapfile
80 ```
81
82 If you get an errors about "holes" or allocations, you'll either need to defrag
83 or try allocating far more space than you need with `dd`, delete the dd file,
84 and create the swap file again.
85
86 ### How to activate swap on boot?
87
88 You need to open `/etc/fstab` and add a line like this:
89
90 ```txt
91 /var/swapfile none swap sw 0 0
92 ```