How to Check Memory Usage In Linux

In Linux server administration, understanding how to monitor and manage memory usage is a critical skill. This comprehensive guide will walk you through the process of checking memory usage on a Linux server, including free memory, used physical memory, swap memory, and buffers used by the kernel. This knowledge is essential for maintaining optimal server performance and troubleshooting issues.

For a deeper understanding of server types, you may want to explore our articles on the best web servers, including Apache, Nginx, and LiteSpeed. Also, consider learning about different hosting types such as dedicated server, VPS server, cloud hosting, and shared hosting.

Checking Memory Usage with the ‘top’ Command

The ‘top’ command is a powerful tool for monitoring system activity in real-time. It provides a dynamic, real-time view of the running system, displaying system summary information and a list of processes currently managed by the Linux kernel.

To use the ‘top’ command, simply type ‘top’ into the terminal or SSH login:

[root@server ~]# top

This command will display a variety of information, including the total number of tasks, the number of running tasks, CPU usage, and detailed memory usage.

top - 23:35:48 up  1:36,  1 user,  load average: 0.00, 0.01, 0.02
Tasks:  67 total,   1 running,  66 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.4%us,  1.8%sy,  0.1%ni, 95.6%id,  2.1%wa,  0.1%hi,  0.0%si,  0.0%st
Mem:    604324k total,   329952k used,   274372k free,    65428k buffers
Swap:  1052248k total,        0k used,  1052248k free,   208324k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    1 root      15   0  2156  640  548 S  0.0  0.1   0:01.03 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    5 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 events/0
    6 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 khelper
    7 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 kthread
   10 root      10  -5     0    0    0 S  0.0  0.0   0:00.01 kblockd/0
   11 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kacpid
  170 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 cqueue/0
  173 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 khubd
  175 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kseriod
  240 root      15   0     0    0    0 S  0.0  0.0   0:00.00 khungtaskd
  241 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pdflush
  242 root      15   0     0    0    0 S  0.0  0.0   0:00.20 pdflush
  243 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kswapd0
  244 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 aio/0

Using the ‘vmstat’ Command to Monitor Memory

The ‘vmstat’ command, short for ‘virtual memory statistics’, provides information about system processes, memory, paging, block IO, traps, and CPU activity.

See also  How to Install Golang on CentOS

To use the ‘vmstat’ command, type ‘vmstat’ into the terminal or SSH login:

[root@server ~]# vmstat

The output of this command provides a snapshot of the system’s memory usage, including swap memory.

procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 273256  65688 208884    0    0    79    39 1039   84  0  2 96  2  0

Displaying Memory Usage with the ‘free’ Command

The ‘free’ command is a simple and easy-to-use tool that displays the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel.

The ‘free’ command can display the output in various units (bytes, KB, MB, or GB) using the -b, -k, -m, or -g options. For instance, to display the total memory in MB, use the following command:

[root@server ~]# free -m -t

The ‘-t’ option adds a line at the bottom of the output, showing the total physical and swap memory.

             total       used       free     shared    buffers     cached
Mem:           590        323        266          0         64        204
-/+ buffers/cache:         54        535
Swap:         1027          0       1027
Total:        1617        323       1294

Checking Memory Usage via ‘/proc/meminfo’

The ‘/proc/meminfo’ file contains real-time information about the system’s memory usage. You can view its contents with the ‘cat’ command:

[root@server ~]# cat /proc/meminfo

The output provides detailed information aboutthe system’s memory, including total memory, free memory, memory used for buffers, and cached memory. It also provides information about swap memory and various other memory-related statistics.

MemTotal:       604324 kB
MemFree:        272512 kB
Buffers:         66040 kB
Cached:         209256 kB
SwapCached:          0 kB
Active:          61864 kB
Inactive:       222596 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:       604324 kB
LowFree:        272512 kB
SwapTotal:     1052248 kB
SwapFree:      1052248 kB
Dirty:            1032 kB
Writeback:           0 kB
AnonPages:        9172 kB
Mapped:           6468 kB
Slab:            39896 kB
PageTables:        972 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:   1354408 kB
Committed_AS:    76248 kB
VmallocTotal:   417784 kB
VmallocUsed:      4052 kB
VmallocChunk:   413304 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
Hugepagesize:     4096 kB

Commands Mentioned

  • top – Displays real-time system summary information and a list of processes currently managed by the Linux kernel.
  • vmstat – Provides information about system processes, memory, paging, block IO, traps, and CPU activity.
  • free – Displays the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel.
  • cat /proc/meminfo – Displays real-time information about the system’s memory usage.
See also  How to Install Bind DNS Server on CentOS and RHEL

Conclusion

Monitoring memory usage is a vital part of Linux server administration. It allows you to understand how your server’s resources are being utilized and helps in identifying any potential issues that might affect performance. The ‘top’, ‘vmstat’, ‘free’, and ‘cat /proc/meminfo’ commands are powerful tools that provide detailed and real-time information about your server’s memory usage.

By understanding how to use these commands effectively, you can ensure that your server is running efficiently and troubleshoot any issues that arise. Whether you’re running a Apache, Nginx, or LiteSpeed server, on a dedicated, VPS, cloud, or shared hosting environment, these skills will be invaluable.

See also  How to Fix "Failed to Install VMware Tools" Error

Remember, regular monitoring and maintenance are the keys to a healthy and high-performing server. Stay tuned for more in-depth tutorials and guides to help you navigate the world of web server administration.

FAQs

  1. What does the ‘top’ command do in Linux?

    The ‘top’ command in Linux provides a dynamic real-time view of the running system. It displays system summary information and a list of processes currently being managed by the Linux kernel, including information about CPU usage and detailed memory usage.

  2. How does the ‘vmstat’ command help in monitoring memory?

    The ‘vmstat’ command, short for ‘virtual memory statistics’, provides information about system processes, memory, paging, block IO, traps, and CPU activity. It gives a snapshot of the system’s memory usage, including swap memory, which can be useful for monitoring and troubleshooting.

  3. What information does the ‘free’ command provide?

    The ‘free’ command in Linux displays the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel. The output can be displayed in various units such as bytes, KB, MB, or GB.

  4. How can I check memory usage via ‘/proc/meminfo’?

    The ‘/proc/meminfo’ file in Linux contains real-time information about the system’s memory usage. You can view its contents with the ‘cat’ command, which provides detailed information about total memory, free memory, memory used for buffers, cached memory, and various other memory-related statistics.

  5. Why is monitoring memory usage important?

    Monitoring memory usage is crucial for maintaining optimal server performance. It helps in identifying processes that are consuming excessive memory, which could slow down or even crash the system. Regular monitoring can help in troubleshooting issues, planning for upgrades, and ensuring efficient resource utilization.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *