Logo

dev-resources.site

for different kinds of informations.

Running npm install on a Server with 1GB Memory using Swap

Published at
7/3/2024
Categories
swap
memory
server
optimization
Author
victorleungtw
Categories
4 categories in total
swap
open
memory
open
server
open
optimization
open
Author
13 person written this
victorleungtw
open
Running npm install on a Server with 1GB Memory using Swap

Running npm install on a server with only 1GB of memory can be challenging due to limited RAM. However, by enabling swap space, you can extend the virtual memory and ensure smooth operation. This blog post will guide you through the process of creating and enabling a swap partition on your server.

What is Swap?

Swap space is a designated area on a hard disk used to temporarily hold inactive memory pages. It acts as a virtual extension of your physical memory (RAM), allowing the system to manage memory more efficiently. When the system runs out of physical memory, it moves inactive pages to the swap space, freeing up RAM for active processes. Although swap is slower than physical memory, it can prevent out-of-memory errors and improve system stability.

Step-by-Step Guide to Enable Swap Space

  1. Check Existing Swap Information

Before creating swap space, check if any swap is already configured:

   sudo swapon --show
Enter fullscreen mode Exit fullscreen mode
  1. Check Disk Partition Availability

Ensure you have enough disk space for the swap file. Use the df command:

   df -h
Enter fullscreen mode Exit fullscreen mode
  1. Create a Swap File

Allocate a 1GB swap file in the root directory using the fallocate program:

   sudo fallocate -l 1G /swapfile
Enter fullscreen mode Exit fullscreen mode
  1. Enable the Swap File

Secure the swap file by setting appropriate permissions:

   sudo chmod 600 /swapfile
Enter fullscreen mode Exit fullscreen mode

Format the file as swap space:

   sudo mkswap /swapfile
Enter fullscreen mode Exit fullscreen mode

Enable the swap file:

   sudo swapon /swapfile
Enter fullscreen mode Exit fullscreen mode
  1. Make the Swap File Permanent

To ensure the swap file is used after a reboot, add it to the /etc/fstab file:

   sudo cp /etc/fstab /etc/fstab.bak
Enter fullscreen mode Exit fullscreen mode

Edit /etc/fstab to include the swap file:

   echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Enter fullscreen mode Exit fullscreen mode
  1. Optimize Swap Settings

Adjust the swappiness value to control how often the system uses swap space. A lower value reduces swap usage, enhancing performance. Check the current value:

   cat /proc/sys/vm/swappiness
Enter fullscreen mode Exit fullscreen mode

Set the swappiness to 15:

   sudo sysctl vm.swappiness=15
Enter fullscreen mode Exit fullscreen mode

Make this change permanent by adding it to /etc/sysctl.conf:

   echo 'vm.swappiness=15' | sudo tee -a /etc/sysctl.conf
Enter fullscreen mode Exit fullscreen mode

Adjust the vfs_cache_pressure value to balance cache retention and swap usage. Check the current value:

   cat /proc/sys/vm/vfs_cache_pressure
Enter fullscreen mode Exit fullscreen mode

Set it to 60:

   sudo sysctl vm.vfs_cache_pressure=60
Enter fullscreen mode Exit fullscreen mode

Make this change permanent:

   echo 'vm.vfs_cache_pressure=60' | sudo tee -a /etc/sysctl.conf
Enter fullscreen mode Exit fullscreen mode

Conclusion

Creating and enabling swap space allows your server to handle memory-intensive operations, such as npm install, more efficiently. While swap is not a substitute for physical RAM, it can provide a temporary solution to memory limitations, ensuring smoother performance and preventing out-of-memory errors. By following the steps outlined above, you can optimize your server's memory management and enhance its overall stability.

server Article's
30 articles in total
Favicon
Singularity: Streamlining Game Development with a Universal Framework
Favicon
VPS Servers for Linux - Everything You Need to Know
Favicon
SLOT THAILAND™ LINK SITUS SLOT GACOR TERBAIK GAMPANG MAXWIN 2024
Favicon
Why adventuresinminecraft.com is the Best Minecraft Server for Customization and Survival
Favicon
Free VPS Hosting and Windows VPS to Understand How They Work on Websites
Favicon
How to Send Emails in Python Using Gmail’s Free SMTP Mail Server API
Favicon
How to write GraphQL resolvers effectively
Favicon
Path-Based Reverse Proxying with Caddy
Favicon
How I Set Up My Own Server (and Why You Should Too)
Favicon
Unlocking the Power of AWS Route 53: Your Complete Guide to DNS Management
Favicon
Unlock Efficient Data Exchange: Import & Export Excel in ASP.NET Core
Favicon
Copy file between server and local machine ( from windows to linux server )
Favicon
Client Boundaries
Favicon
Server actions in Next.js
Favicon
Self-host - Part 3 - MySQL and PostgreSQL Database Backup to Local Drive
Favicon
need suggestions
Favicon
SQL Operators Made Easy for Beginners
Favicon
What is Localhost in Development Mode?
Favicon
How to Customize the Fastify Logger
Favicon
Websocket starter in Rust with client and server example
Favicon
Unraid: Das ultimative Tool für deine Heimserver 🚀
Favicon
Drawbacks to Using Rack Server Unit as Desktop Computer?
Favicon
How to View Your index.php File in a Browser
Favicon
NextJs Server Actions: Why and How
Favicon
Self-host - Part 2 - Zero-Downtime Deployment using Docker Swarm
Favicon
Decoding Web Hosting: Understanding the landlords of the digital Realm Landscape
Favicon
Kaptan ve Tayfa - Kaptan Sunucu Kurulumu
Favicon
argc, argv의 차이
Favicon
Server side (vulnerability scanning)
Favicon
Running npm install on a Server with 1GB Memory using Swap

Featured ones: