Solving the ‘Segmentation Fault’ Error on Linux

The Segmentation Fault error (often called just segfault) is usually related to a memory issue whereby a process tries to access unallocated memory that does not belong to it, and the Linux kernel ends up terminating the process and issuing the error as a means to mitigate further issues. It can be tricky to diagnose and usually does not give us much to go off of, so we must dig through log files and try a variety of methods to figure out the root problem. In this tutorial, we will go through step by step instructions to solve the Segmentation Fault error on a Linux system.

In this tutorial you will learn:

  • How to check dmesg for segfault entries
  • How to check RAM usage
Solving the 'Segmentation Fault' Error on Linux
Solving the ‘Segmentation Fault’ Error on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user
From personal experience, this error is more likely to occur on a system with sparse memory resources. A good example is running a cheap VPS that only has 1 or 2 GB of RAM, and trying to host several services at the same time. It is not surprising to encounter the segfault error in this scenario, as resource starved processes are more likely to grab memory that is simply not available.

Solving the ‘Segmentation Fault’ Error on Linux

  1. The first thing we should do is figure out which program is causing the error to occur. You can look through the system logs for clues, and in particular should check dmesg. Once you know which program is causing the issue, you can try different configurations to prevent it or, if possible, get rid of it altogether. Here is a real example from a gaming server:
    $ dmesg
    [10541938.808655] srcds_linux[14224]: segfault at 0 ip 00000000f73695d9 sp 00000000ffad8b80 error 6 in[f7165000+32a000]

    This line tells us that the srcds_linux program tried to allocate some memory that did not belong to it, and thus was terminated with a Segmentation Fault error. Notice how the log calls this a segfault error instead, so be sure to look for both terms.

  2. Since Segmentation Fault typically means we are running out of usable memory that programs are requesting, we can check to see which processes are taking up the most memory with the top or htop command. This will tell us if a process is consuming more memory than it should, and we can eliminate the process or take further steps to make sure it has the memory it needs to run properly.
    $ htop

    There are plenty of other commands we can use to monitor RAM usage, which we cover in detail in this tutorial: How to Monitor RAM Usage on Linux. Or you can use a Bash script to monitor RAM usage.

  3. If you are sure that your system has plenty of RAM installed, and you do not have any offending processes trying to consume more memory than they should, then you could be receiving the error due to errors in your RAM. Try running an error checking utility like memtest86 which will check every part of your installed RAM to see if there are any errors. If there are, then you will need to replace the bad RAM stick.
  4. Another possible cause for the Segmentation Fault error is a software bug. Outdated programs or those in beta should be updated to current versions to see if the problem can be fixed. You can also check online to see if other users are reporting similar issues with the same version of the software that you are running. If you can’t update the program, you can try running a debugging tool to see if it indicates any memory leaks or other issues.

Closing Thoughts

In this tutorial, we saw how to solve the Segmentation Fault error on a Linux system. This is a frustrating error which confuses most users the first time, as it gives us absolutely no information about what the underlying cause could be. Once you realize that it is a memory related issue, we can take steps to ensure that our system has ample memory installed to run the services on it, and that there are no offending processes trying to consume more RAM than they should.

Comments and Discussions
Linux Forum