Hello Guys,
Yesterday when i was working, my Ubuntu stop responding(Freeze) and was unable to perform any task. I checked my RAM status and it was nearly 100%. After some research, i found the solution and I think its worthwhile to share this, since this is very common problem with relatively simple solution, but can waste your lot of time if doesn’t handle in proper way. So below are the reasons and solution for the same. Hopefully it would be beneficial for you.
Reason:
The Ubuntu operating system gets freeze due to insufficient RAM available, because there are no space available to open other applications. Basically by default, swap file gets created in the file system in OS installation time itself, but due to some reason, it get removed, which ultimately raise the problem of freezing the operating system.
The Swap Space(Swap File) on the hard drive will be used mainly, when there is no sufficient space left in the RAM to hold in-use application data.
The information written to disk will be significantly slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap file for the older data. Overall, having swap file as a fallback is a good safety net against out-of-memory exceptions on systems with non-SSD storage available.
You can check your Swap file in your system monitor application.
Solution.
STEP 1 – Check if the system has configured swap file.
1 |
sudo swapon --show |
If you don’t get back any output, this means your system does not have Swap file available. Currently, you can verify that there is no active swap file using the free
utility:
1 |
free -h |
1 2 3 4 |
Output total used free shared buff/cache available Mem: 985M 84M 222M 680K 678M 721M Swap: 0B 0B 0B |
As you can see in the output (Swap row), there is no swap active on the system.
STEP 2– Checking Availability of space on the hard drive Partition, so we can create the swap file.
1 |
df -h |
1 2 3 4 5 6 7 8 9 10 |
Output Filesystem Size Used Avail Use% Mounted on udev 481M 0 481M 0% /dev tmpfs 99M 656K 98M 1% /run /dev/vda1 25G 1.4G 23G 6% / tmpfs 493M 0 493M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 493M 0 493M 0% /sys/fs/cgroup /dev/vda15 105M 3.4M 102M 4% /boot/efi tmpfs 99M 0 99M 0% /run/user/1000 |
In the output, the device with /
in the “Mounted on"
column is our disk in this case. We have plenty of space available in this example (only 1.4G used). Your usage will probably be different.
STEP 3 – Creating a swap file
If you have 4GB of ram on your system, then ideally, you should create a swap file of 4GB.
1 |
sudo fallocate -l 4G /swapfile |
We can verify that the correct amount of space was reserved by typing:
1 |
ls -lh /swapfile |
1 |
-rw-r--r-- 1 root root 4.0G Apr 25 11:14 /swapfile |
Our file has been created with the correct amount of space set aside.
STEP 4- Enabling the swap file.
Make the file only accessible to root by typing:
1 |
sudo chmod 600 /swapfile |
We can now mark the file as swap space by typing:
1 |
sudo mkswap /swapfile |
1 2 3 |
Output Setting up swapspace version 1, size = 4096 MiB (4073737728 bytes) no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf |
After marking the file, we can enable the swap file, allowing our system to start utilizing it:
1 |
sudo swapon /swapfile |
Verify that the swap is available by typing:
1 |
sudo swapon --show |
1 2 3 |
Output NAME TYPE SIZE USED PRIO /swapfile file 4096M 0B -2 |
STEP – 5 Making swap file permanent
Our recent changes have enabled the swap file for the current session. However, if we reboot, the OS will not retain the swap settings . We can change this by adding the swap file to our /etc/fstab
file.
Back up the /etc/fstab
file in case anything goes wrong:
1 |
sudo cp /etc/fstab /etc/fstab.bak |
Add the swap file information to the end of your /etc/fstab
file by typing:
1 |
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab |
Now we have successfully created a swap space. Hopefully, the problem of Ubuntu Freeze will also go away.
Please comments and share your experience with us to make us better next time.
Recent Comments