Clickbait version: Increase your server’s system memory for free! 😀
Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space.[src]
Note: Not recommended to be used in production environment. Swap space is very slow compared to physical memory and can seriously affect your application performance.
It’s fairly common to run into “System.OutOfMemoryException” if you are running a memory heavy application or multiple applications on a remote server. To mitigate this issue you can simply create a Swap space for your server and use your disk space as system memory, this will be a little slower but you wouldn’t have to upgrade your server to higher memory one if you just need some MBs of extra ram or maybe need to perform a single task which needs some extra memory.
To enable 2GB of swap space use the following commands
SWAPFILE=/var/swapfile SWAP_MEGABYTES=2048 sudo /bin/dd if=/dev/zero of=$SWAPFILE bs=1M count=$SWAP_MEGABYTES sudo /bin/chmod 600 $SWAPFILE sudo /sbin/mkswap $SWAPFILE sudo /sbin/swapon $SWAPFILE
If you are using elastic beanstalk in staging/test environments and frequently run out of system memory during deployments the here’s your config file.
Just create a setup-swap.config file in .ebextensions folder and you are done.
commands: create-post-dir: command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post" ignoreErrors: true files: "/opt/elasticbeanstalk/hooks/appdeploy/post/setup_swap.sh": mode: "000755" owner: root group: root content: | #!/usr/bin/env bash SWAPFILE=/var/swapfile SWAP_MEGABYTES=50 echo "configuring swap storage" if [ -f $SWAPFILE ]; then echo "Swapfile $SWAPFILE found, assuming already setup" exit; fi echo "creating swapfile" /bin/dd if=/dev/zero of=$SWAPFILE bs=100M count=$SWAP_MEGABYTES /bin/chmod 600 $SWAPFILE /sbin/mkswap $SWAPFILE /sbin/swapon $SWAPFILE echo "swap configuration complete"