- 1 1. Introduction
- 2 2. Basic Memory Management in Ubuntu
- 3 3. Practical Methods to Free Memory
- 4 4. Using Automatic Memory Management Tools
- 5 5. Important Considerations
- 6 6. FAQ
- 6.1 Q1. Do I need to free memory frequently?
- 6.2 Q2. Does deleting cache slow down the system?
- 6.3 Q3. What is the benefit of increasing swap space?
- 6.4 Q4. Are there automatic ways to free memory?
- 6.5 Q5. Can freeing memory make the system unstable?
- 6.6 Q6. Is zRAM useful for all systems?
- 6.7 Q7. Why don’t I see improvement after freeing memory?
- 7 7. Conclusion
1. Introduction
Ubuntu is a popular open-source Linux distribution used by many users. However, after the system runs for an extended period, memory may gradually become insufficient. This issue often occurs because cache and unnecessary processes occupy memory resources.
This article explains concrete methods to free up memory in Ubuntu and improve system performance. It targets beginners to intermediate users and provides solutions using actual command examples and scripts. If you understand basic Ubuntu operations, you can easily apply the steps.
Benefits of Reading This Article
- Understand how memory works.
- Learn practical techniques to free up memory in Ubuntu.
- Get optimization tips to improve overall system performance.
2. Basic Memory Management in Ubuntu
Types of Memory and Their Roles
Memory management in Ubuntu consists of the following three primary components:
- RAM (Physical Memory)
Temporary storage for programs and data. Because it directly affects processing speed, having adequate capacity is essential. - Cache Memory
Temporary data storage used to accelerate access to frequently used programs and files. Although cache improves speed, excessive accumulation may cause RAM shortages. - Swap Space
Storage used when RAM runs out. Since HDDs and SSDs are slower than RAM, relying too heavily on swap results in performance degradation.
How to Check Current Memory Usage
Use the following commands to check memory usage.
free -h Command
free -h
This command displays memory usage in a human-readable format.
Example Output:
total used free shared buff/cache available
Mem: 7.7G 2.5G 1.8G 1.2G 3.4G 4.0G
Swap: 2.0G 0B 2.0G
- total: Total memory
- used: Memory currently in use
- free: Unused free memory
- buff/cache: Memory allocated to cache
- available: Memory actually available for use
htop Tool
For real-time monitoring of memory usage, htop is useful.
- Install:
sudo apt install htop
- Run:
htop
It provides a colorful interface that displays CPU and memory usage in real time.
3. Practical Methods to Free Memory
3.1 Clearing Page Cache
What Is Page Cache?
Page cache temporarily stores data in memory to speed up file and program access. While useful, freeing cache can help obtain memory resources when RAM is insufficient.
How to Clear Cache
Follow the steps below:
- Cache Clearing Command
Run the following command to clear the cache:
sudo sync && sudo sysctl -w vm.drop_caches=3
sync: Synchronizes data to disk.sysctl -w vm.drop_caches=3: Clears page cache.
- Verification
Usefree -hbefore and after execution to compare memory usage.
Notes
- Clearing cache may temporarily slow down the system.
- Cache is automatically managed, so frequent clearing is unnecessary.
3.2 Optimizing Swap Space
What Is Swap?
Swap space stores data temporarily when RAM is full. Because disk access is slower than RAM, swap usage may reduce performance.
Check Swap Usage
Run the following command to check current swap usage:
swapon --show
Adding Swap Space
If swap space is insufficient, create a new swap file.
- Create a Swap File
sudo fallocate -l 1G /swapfile
This creates a 1GB swap file.
- Set Permissions
sudo chmod 600 /swapfile
- Enable Swap
sudo mkswap /swapfile
sudo swapon /swapfile
- Verification
Executeswapon --showagain to confirm the new swap space.
Releasing Swap Space
sudo swapoff -a && sudo swapon -a
This moves swap data back into RAM.
3.3 Terminating Unnecessary Processes
What Are Unnecessary Processes?
Unused processes that consume large amounts of memory can be identified and terminated to free memory resources.
How to Identify Processes
Use htop or ps aux to identify memory-heavy processes.
ps auxCommand
ps aux --sort=-%mem | head
Displays memory-consuming processes in descending order.
htopCommand
- Allows you to interactively inspect and terminate processes.
Terminate a Process
Identify the process ID (PID) and run:
sudo kill -9 <PID>
4. Using Automatic Memory Management Tools
4.1 Configuring zRAM
What Is zRAM?
zRAM uses compressed memory to virtually increase RAM capacity. It is faster than disk-based swap and helps resolve memory shortages efficiently.
Install and Configure zRAM
- Install zRAM
Ubuntu provides a dedicated configuration tool in its repositories.
sudo apt install zram-config
- Confirm zRAM Status
After installation, it is enabled automatically. Verify using:
swapon --show
If /dev/zram0 appears in the results, it is active.
- Custom Settings
Adjust settings by editing the configuration file:
sudo nano /etc/default/zram-config
Modify compression size or parameters as needed, then reboot.
Advantages of Using zRAM
- Reduces disk access and improves responsiveness.
- Significantly reduces swap usage.
4.2 Creating an Automatic Memory Cleanup Script
Create a Simple Script
Use the following shell script to automate memory cleanup:
- Script Content
#!/bin/bash
sync && echo 3 > /proc/sys/vm/drop_caches
echo "Memory freed: $(date)"
This script clears page cache and logs the time of execution.
- Save the Script
Save it asmemory_cleanup.sh.
nano ~/memory_cleanup.sh
Paste the code and save.
- Grant Execution Permission
chmod +x ~/memory_cleanup.sh
- Manual Execution
sudo ~/memory_cleanup.sh
Set Automatic Execution
Use cron to run the script periodically.
- Edit cron settings
crontab -e
- Add a Job
Run the script every hour:
0 * * * * sudo ~/memory_cleanup.sh
- Confirm Registration
crontab -l
4.3 Notes
- zRAM consumes CPU resources: Compression requires CPU power, which may affect low-spec systems.
- Script execution frequency: Running scripts too often may reduce performance.

5. Important Considerations
5.1 Understanding Cache Deletion Impact
Risks of Cache Deletion
- Cache accelerates system performance, so deleting it may temporarily slow access.
- In environments with heavy database or file access, cache deletion may have negative effects.
When Deletion Is Necessary
Only delete cache when physical memory is critically low and new processes cannot start due to insufficient resources.
5.2 Managing Swap Space
Excessive Swap Usage
Frequent swap use increases disk I/O and reduces responsiveness.
Recommended Swap Size
Optimal size depends on system usage and RAM capacity:
- RAM ≤ 2GB: Swap size around twice RAM.
- RAM ≥ 2GB: Swap equal to or less than RAM.
Frequency of Swap Release
Avoid clearing swap too frequently; repeated operations may degrade performance.
5.3 Risks When Terminating Processes
Identifying Unnecessary Processes
Terminating essential processes may crash applications or destabilize the system.
Safe Termination Method
Use htop to confirm before terminating. For force termination:
kill -9 <PID>
Use kill -9 with caution as it forces termination.
5.4 Notes When Using zRAM
Increased CPU Load
Because zRAM relies on compression, low-performance CPUs may experience overhead.
Performance Monitoring
Monitor system performance regularly using htop or free.
5.5 Notes for Automated Scripts
Execution Frequency
Running cleanup scripts too often may reduce performance. Execute every 1–2 hours when needed.
Log Recording
Recording script logs makes troubleshooting easier.
#!/bin/bash
sync && echo 3 > /proc/sys/vm/drop_caches
echo "Memory cleanup: $(date)" >> /var/log/memory_cleanup.log
6. FAQ
Q1. Do I need to free memory frequently?
A: No. Ubuntu manages memory automatically. Only free memory when shortages cause performance issues.
Q2. Does deleting cache slow down the system?
A: It may temporarily slow performance because cache speeds up access. However, clearing it frees resources for new processes.
Q3. What is the benefit of increasing swap space?
A: It prevents crashes when RAM becomes insufficient. But excessive swap usage can reduce performance.
Q4. Are there automatic ways to free memory?
A: Yes. Use scripts with cron or introduce zRAM to manage memory automatically.
Q5. Can freeing memory make the system unstable?
A: Not if done correctly. Avoid terminating essential processes or clearing cache too frequently.
Q6. Is zRAM useful for all systems?
A: zRAM benefits systems with limited RAM. On high-end machines, effects may be minimal.
Q7. Why don’t I see improvement after freeing memory?
- Cache may have already been cleared.
- Swap usage was minimal.
- Other bottlenecks, such as CPU or disk I/O, may exist.
7. Conclusion
This article explained how to free memory in Ubuntu—from fundamentals to actionable solutions. Memory shortages can significantly reduce system performance, but proper management and freeing techniques can resolve these issues effectively.
Key Takeaways
- How Ubuntu Manages Memory
- Memory consists of RAM, cache, and swap—each with a distinct purpose.
- Effective Memory Release Methods
- Clear Page Cache using
syncandvm.drop_caches. - Manage Swap Space by adding or releasing swap files.
- Terminate Unnecessary Processes safely using monitored tools.
- Automation Tools
- Use zRAM and scripts to automate memory management.
- Important Notes
- Cache and swap operations may affect system performance—use cautiously.
- FAQ Support
- The FAQ section addresses common user questions for clarity.
Future Recommendations
Memory management in Ubuntu is not limited to freeing resources. Regular monitoring and proper allocation are key strategies for maintaining performance.
- Establish System Monitoring Habits
Regularly check memory withhtopandfree. - Improve Memory Efficiency
Disable unused processes and review overall system practices. - Use Tools
Apply zRAM and automation scripts to reduce workload.
Use this guide to manage your Ubuntu system efficiently and maintain stable performance. Continuous optimization and appropriate resource management are crucial for a comfortable working environment.