Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

Publication

Supercharging Your Linux VM for Data Science: A Guide to Boosting Linux VM RAM for Improved Performance
Latest   Machine Learning

Supercharging Your Linux VM for Data Science: A Guide to Boosting Linux VM RAM for Improved Performance

Last Updated on August 15, 2023 by Editorial Team

Author(s): Prithivee Ramalingam

Originally published on Towards AI.

Ever been stuck while using DistilBERT due to an “Out of Memory” error?

I’ve been there, and I’ve found a game-changing solution: swap files.

Swap files provide an ingenious solution to the challenge of insufficient RAM by extending the system’s memory capacity onto disk space.

Over the course of the article, we’ll understand what swap files are and delve into the step-by-step process of creating and utilizing swap files.

Image Source

What are swap files?

Swap files serve as a safety net for memory-hungry tasks. Imagine your computer’s memory (RAM) as a workspace where it temporarily stores data that it needs to work with immediately. However, there are instances when this workspace becomes too crowded, especially when dealing with transformer-based models. This is where swap files come into play. They act as a supplementary space, residing on your computer’s storage drive, that steps in when the RAM is fully occupied. When the RAM reaches its limit, less frequently used data is moved from the RAM to the swap file, freeing up valuable RAM space for ongoing tasks.

Though swap files are slower to access compared to RAM, they prevent your system from crashing due to memory exhaustion, ensuring smoother operation even under heavy workloads.

Setting up swap files involves designating a portion of your storage space for this purpose. Once configured, the operating system manages the swapping process automatically, optimizing memory usage. However, it’s worth noting that while swap files are a beneficial solution for avoiding crashes, they should not be solely relied upon to substitute for physical RAM.

In essence, swap files strike a balance between efficient memory utilization and the potential slowdown caused by swapping data between RAM and storage, making them a crucial tool for maintaining system stability and performance.

Step-by-Step Guide:

Here’s a step-by-step guide to increasing the RAM using a swap file concept in a Linux VM.

1. Check Current RAM and Swap:

Before creating a swap file, it’s good to know the current RAM and swap usage. You can use the free command to check this:

free -h

As we can see from the below image, we have a memory of 1 GB and currently we don't have any swap memory.

Image Source — Check RAM and Swap memory

2. Create a Swap file:

The below bash command is used to create a swap file in a Linux system.

sudo fallocate -l 2G /swapfile

Let’s break down what each part of the command does:

  • sudo: This command is used to execute the subsequent command with superuser privileges, allowing you to perform administrative tasks.
  • fallocate: This is a command used to allocate space for a file. In this case, it's being used to create the swap file.
  • -l 2G: This part specifies the size of the file to be created. Here, it's setting the size to 2 gigabytes (2G). You can adjust this value to allocate more or less space for your swap file.
  • /swapfile: This is the path and name of the file that will be created. In this case, the swap file will be named "swapfile" and will be created in the root directory (/).

So, when you run the command, it creates a swap file named “swapfile” with a size of 2 gigabytes in the root directory of your Linux system. This swap file can then be used as virtual memory to supplement your system’s physical RAM.

To make sure that everyone can access the swap file, we can use the below bash command.

sudo chmod 600 /swapfile

3. Setup the specified file as swap space:

The below bash command is used to set up the specified file as a swap space in a Linux system.

sudo mkswap /swapfile

Let’s break down what this command does:

  • mkswap: This is a command that is used to set up a swap area on a device or file. In this case, it's being used to configure the "/swapfile" as a swap area.
  • /swapfile: This is the path and name of the file that you want to set up as a swap space.

When you run this command, it prepares the “/swapfile” to be used as a swap area. This means that the file is formatted and organized in a way that the Linux system can efficiently use it for storing data that would normally be stored in RAM. This enables your system to extend its memory capacity by utilizing the swap space to handle situations where physical RAM is insufficient for running all the active processes.

4. Activate swap space:

The below bash command is used to activate a swap space in a Linux system.

sudo swapon /swapfile

Let’s break down what this command does:

  • swapon: This is a command used to activate swap devices or files. It enables the specified swap area to start being used as virtual memory.
  • /swapfile: This is the path and name of the swap file that you want to activate.

When you run this command, it activates the “/swapfile” as a swap area, making it available for use by the system as an extension of its physical RAM. This means that data can now be swapped between the swap space and the RAM, allowing the system to manage memory more efficiently and handle tasks that require more memory than is physically available.

5. Display a list of active swap areas:

The below bash command is used to display a list of active swap areas on a Linux system.

sudo swapon - show

When you run this command with superuser privileges using sudo, it will show you the details of the active swap areas, including the name, type, size, and other relevant information. This can be helpful for verifying that your swap file or other swap devices are correctly activated and in use by the system for virtual memory management.

From the below image, it is verified that we have created a swap file of size 2 GB.

Image Source — To display active swap areas in the VM

We can use the same free -hcommand to have a clear visualization of the RAM and Swap Memory. As we can see a Swap memory of 2 GB is created as we desired.

Image Source — Check RAM and Swap memory

Congratulations! You have successfully enhanced your Linux VM’s performance through the power of swap files! By mastering the creation, configuration, and activation of swap files, you’ve equipped yourself with a valuable tool to overcome memory limitations and elevate your data science projects to new heights. Keep up the great work!

Reference

https://www.udemy.com/course/nlp-with-bert-in-python/

Want to Connect?

If you have enjoyed this article, please follow me here on Medium for more stories about machine learning and computer science.

Linked In — Prithivee Ramalingam U+007C LinkedIn

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓