Support >
  About cloud server >
  The correct approach and methods for managing memory on a Singapore VPS cloud server

The correct approach and methods for managing memory on a Singapore VPS cloud server

Time : 2025-12-22 17:17:21
Edit : DNS.COM

When managing a Singapore VPS (Virtual Private Server), seeing the control panel or the `free -h` command showing memory usage close to 90% or even higher often leads many users to immediately think, "I need to clean up the memory." However, in Linux systems, this intuition is often misleading. Unlike systems like Windows, the Linux kernel's memory management philosophy is that "free memory is wasted memory." It actively utilizes memory not currently used by programs to cache disk data and buffer writes, significantly improving system performance. Therefore, high memory usage in Linux is usually a sign of high performance, not a problem. What you really need to be wary of and intervene in is when available memory is about to run out and the system starts frequently using the swap partition. This article will guide you through understanding the Linux memory mechanism and teaching you how to properly release memory when it's truly needed.

Managing a Singapore VPS, seeing the control panel or the `free -h` command showing memory usage close to 90% or even higher often leads many users to immediately think, "I need to clean up the memory." However, in Linux systems, this intuition is often misleading. Unlike systems like Windows, the Linux kernel's memory management philosophy is that "free memory is wasted memory." It proactively utilizes memory not currently used by programs to cache disk data and buffer writes, significantly improving system performance.

Therefore, high memory usage in the Linux world is usually a sign of high performance, not a problem. What you really need to be wary of and intervene in is when available memory is about to run out and the system starts frequently using the swap partition. This article will guide you through understanding the Linux memory mechanism and teaching you how to properly release memory when it's truly needed.

First, we need to correctly interpret memory states. Open a terminal and type `free -h`. You will see output similar to the following:

total used free shared buff/cache available

Mem: 2.0Gi 1.2Gi 150Mi 30Mi 650Mi 600Mi

Swap: 1.0Gi 200Mi 800Mi

The key metric is "available," which represents the system's estimated available memory for starting new applications without swapping. As long as this value is sufficient (e.g., more than 500MB in 2GB of total memory), there's no need to worry even if "used" is high. "Buff/cache" is the portion used by the kernel for caching, and it is automatically and quickly released when programs need more memory. Therefore, in most cases, you don't need to manually clean it up.

The real problem arises when "available" memory is extremely low and "swap" usage starts to steadily increase. This leads to a significant performance drop because disk swapping is several orders of magnitude slower than memory. At this point, what you need to do is not simply "clean up," but diagnose and address the root cause. The first step is to identify the culprit consuming excessive memory. The `top` or `htop` command is the best tool. After launching `top`, press `M` (uppercase) to sort by memory usage. You will see a list of processes; pay close attention to the `%MEM` and `RES` (resident memory) columns. A common culprit is an out-of-control application, such as a Java service, a database (MySQL/MongoDB), or a custom script.

Once the specific process is located, the troubleshooting approach depends on the type of process:

1. If it's a non-critical and abnormal program: Try terminating it using `kill [PID]`, or force termination using `kill -9 [PID]`.

2. If it's a core service (such as MySQL): Blindly terminating it will cause service interruption. In this case, check its configuration. For example, if MySQL's `innodb_buffer_pool_size` is set too high, it will monopolize a large amount of memory. You need to adjust its configuration file (such as `/etc/mysql/my.cnf`), optimize memory parameters, and then restart the service.

3. If it's a memory leak: Some programs may continuously allocate but not release memory over time. In this case, restarting the service is an immediate solution, such as `sudo systemctl restart nginx`. In the long run, software updates or code fixes are needed.

After completing the above targeted remediation, if you still want to actively release the kernel cache (buff/cache) to quickly restore "available" memory (e.g., before and after running a particularly memory-intensive batch task), Linux provides a standard kernel interface. This is achieved by writing a specific value to `/proc/sys/vm/drop_caches`. Note that this is typically only done in specific testing or debugging scenarios, as clearing the cache may cause a short-term increase in disk I/O, impacting performance.

# First, execute the `sync` command to write dirty pages to disk, ensuring data safety.

sync

# Then, enter a number from 1 to 3 to select the cleanup level.

echo 1 > /proc/sys/vm/drop_caches # Clear only the page cache (PageCache)

# Or

echo 2 > /proc/sys/vm/drop_caches # Clear directory entries and inode cache

# Or

echo 3 > /proc/sys/vm/drop_caches # Clear all caches from 1 and 2

After execution, you will see a significant decrease in the "buff/cache" value in `free -h`, and a corresponding increase in the "available" value. This does not release memory used by applications.

Besides temporary cleanup, establishing monitoring and automated response mechanisms is more important. You can write a simple shell script to periodically check memory and swap partition usage and automatically alert or take minor measures when thresholds are exceeded.

#!/bin/bash

# Example memory monitoring script

# Set thresholds (percentage)

SWAP_THRESHOLD=20 # Alarm when swap space usage exceeds 20%

AVAIL_THRESHOLD=100 # Alarm when available memory is below 100MB

# Get current memory data (in KB)

MEM_INFO=$(free -k | awk 'NR==2{print $6, $7}')

AVAIL_KB=$(echo $MEM_INFO | awk '{print $2}')

SWAP_USED=$(free -k | awk 'NR==3{print $3}')

# Convert to MB

AVAIL_MB=$((AVAIL_KB / 1024))

# Check and perform operations

if [ $AVAIL_MB -lt $AVAIL_THRESHOLD ]; then

echo "$(date) Warning: Low available memory (${AVAIL_MB}MB)" >> /var/log/memory_watch.log

# This can be used to integrate alerts such as sending emails and DingTalk messages.

fi

Add this script to a cron job for automated monitoring.

In short, the correct way to "clean up memory" on a Singapore VPS cloud server is not to find a magic command to instantly free up memory, but to understand Linux's "use-first" memory philosophy, learn to use `free -h` and `top` for professional diagnosis, precisely manage problematic processes, and safely release cache when necessary.

DNS Becky
DNS Luna
DNS Amy
DNS NOC
Title
Email Address
Type
Information
Code
Submit