HowTo: Find Out Top Processes By Memory Usage In Linux

It is quite a common situation when your server is out of memory and you want to check what processes are using all the RAM and swap.

In this small note you’ll find two similar commands that can find out and sort top processes by memory usage on your Linux system.

I’ve successfully used these commands on: Linux Mint, Ubuntu, Debian, CentOS, RHEL.

Use any of the following commands to display top 10 processes (including child processes) that use the most memory on your Linux machine.

Show What Processes Are Using All The RAM in Linux

Use the following command to find out top processed sorted by memory usage, in megabytes (MB):

ps axo rss,comm,pid \
| awk '{ proc_list[$2]++; proc_list[$2 "," 1] += $1; } \
END { for (proc in proc_list) { printf("%d\t%s\n", \
proc_list[proc "," 1],proc); }}' | sort -n | tail -n 10 | sort -rn \
| awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}'

Example: Just copy/paste the above command to your terminal and press ENTER to display top 10 processes, sorted by memory usage. You should get the similar output:

admin@phantom ~ $ ps axo rss,comm,pid \
| awk '{ proc_list[$2]++; proc_list[$2 "," 1] += $1; } \
END { for (proc in proc_list) { printf("%d\t%s\n", \
proc_list[proc "," 1],proc); }}' | sort -n | tail -n 10 | sort -rn \
| awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}'

1047MB	firefox
648MB	VirtualBox
177MB	thunderbird
119MB	Xorg
82MB	python
82MB	remmina
60MB	pidgin.orig
48MB	caja
47MB	apache2
33MB	puppet

Display Processes Sorted By Memory Usage in Linux

Use the following command to display processes that are using all the memory, in megabytes (MB):

ps axo rss,comm,pid \
| awk '{ proc_list[$2] += $1; } END \
{ for (proc in proc_list) { printf("%d\t%s\n", proc_list[proc],proc); }}' \
| sort -n | tail -n 10 | sort -rn \
| awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}'

Example: Just copy/paste the above command to your terminal and press ENTER to check what processes are using all the RAM and swap. You should get the similar output:

admin@phantom ~ $ ps axo rss,comm,pid \
| awk '{ proc_list[$2] += $1; } END \
{ for (proc in proc_list) { printf("%d\t%s\n", proc_list[proc],proc); }}' \
| sort -n | tail -n 10 | sort -rn \
| awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}'

1022MB	firefox
648MB	VirtualBox
177MB	thunderbird
119MB	Xorg
82MB	python
82MB	remmina
60MB	pidgin.orig
48MB	caja
47MB	apache2
33MB	puppet

Check a Website Response Time from the Linux Command Line

Test you website response time from the Linux command line with CURL.

Total website response time

Use the following command to get a total response time, in seconds.

$ curl -s -w %{time_total}\\n -o /dev/null http://www.shellhacks.com

Sample output:

0,117

Brief options description:

Option Description
-s Quiet mode. Don’t show progress meter or error messages
-w Defines what to display on stdout after a completed and successful operation
-o Write output to ‘/dev/null’
time_total The total time, in seconds, that the full operation lasted

Detailed timing of a website response

The following command returns lookup, connect, pretransfer, starttransfer time in seconds and the total time that the full operation lasted.

$ curl -s -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null http://www.shellhacks.com

Sample output:

Lookup time:    0,004
Connect time:   0,022
PreXfer time:   0,022
StartXfer time: 0,068

Total time:     0,125

Brief options description:

Option Description
Lookup time (time_namelookup) The time, in seconds, it took from the start until the name resolving was completed
Connect time (time_connect) The time, in seconds, it took from the start until the TCP connect to the remote host was completed
PreXfer time (time_pretransfer) The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all ‘pre-transfer’ commands and negotiations that are specific to the particular protocol(s) involved
StartXfer time (time_starttransfer) The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes ‘time_pretransfer’ and also the time the server needed to calculate the result

More detailed timing of a website response

The following command adds appconnect and redirect time in seconds, to the previous report. These options are available in a latest versions of CURL.

$ curl -s -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nAppCon time:\t%{time_appconnect}\nRedirect time:\t%{time_redirect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null http://www.shellhacks.com

Sample output:

Lookup time:    0,003
Connect time:   0,020
AppCon time:    0,000
Redirect time:  0,000
PreXfer time:   0,020
StartXfer time: 0,963

Total time:     1,001

Brief options description:

Option Description
AppCon time (time_appconnect) The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed (Added in 7.19.0)
Redirect time (time_redirect) The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. ‘time_redirect’ shows the complete execution time for multiple redirections. (Added in 7.12.3)

Use --version to see if your CURL supports these options.

$ curl --version

Response Times: The 3 Important Limits

Short note for your information.

  • 0.1 second – is about the limit for having the user feel that the system is reacting instantaneously, meaning that no special feedback is necessary except to display the result;
  • 1.0 second – is about the limit for the user’s flow of thought to stay uninterrupted, even though the user will notice the delay. Normally, no special feedback is necessary during delays of more than 0.1 but less than 1.0 second, but the user does lose the feeling of operating directly on the data;
  • 10 seconds – is about the limit for keeping the user’s attention focused on the dialogue. For longer delays, users will want to perform other tasks while waiting for the computer to finish, so they should be given feedback indicating when the computer expects to be done. Feedback during the delay is especially important if the response time is likely to be highly variable, since users will then not know what to expect.

HowTo: TEST Internet Speed via Linux Command Line

If you need to troubleshoot an issue with slow Internet access on your Linux system, it would be a good idea to measure the current Internet speed.

In this article you’ll find how to test Internet speed from the Linux terminal, using speedtest_cli – a simple command-line client written in Python for measuring Internet bandwidth by using Speedtest.net infrastructure.

I’ll show how to simply check download/upload Internet speed to the geographically closest server, how to measure the network speed to specific server and how to share the results of speed test via Speedtest.net.

All these tasks can be performed using the Linux command line only.

Test Download/Upload Internet Speed in Linux

Use the following command to Test Internet Download and Upload Speed from the Linux Command Line:

$ wget -O - https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python

Sample output:

Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from ******* (xxx.xxx.xxx.xxx)...
Selecting best server based on latency...
Hosted by ******* [1.24 km]: 2.458 ms
Testing download speed...
Download: 84.35 Mbits/s
Testing upload speed...
Upload: 77.32 Mbits/s

Test Internet Speed From the Linux Command Line

If you are planing to measure Internet bandwidth often, you can install speedtest script on your system:

$ wget https://raw.github.com/sivel/speedtest-cli/master/speedtest.py
$ chmod a+rx speedtest.py
$ sudo mv speedtest.py /usr/local/bin/speedtest
$ sudo chown root:root /usr/local/bin/speedtest

After installation, you will be able to test Internet speed from the terminal by simply running speedtest command, as follows:

$ speedtest
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from ******* (xxx.xxx.xxx.xxx)...
Selecting best server based on latency...
Hosted by ******* [1.24 km]: 2.458 ms
Testing download speed...
Download: 84.35 Mbits/s
Testing upload speed...
Upload: 77.32 Mbits/s

Share the Result Of Speed Test via Speedtest.net

If you want to share the result of speed test, run speedtest command, as follows:

$ speedtest --share
Share results: http://www.speedtest.net/result/3690948322.png

Get the List Of Available Speedtest.net Servers

Run the following command to get the list of all available Speedtest.net servers around the world:

$ speedtest --list | more
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
3464) Vodafone España (Alicante, Spain) [3047.78 km]
 804) ServiHosting Networks (Elda, Spain) [3063.22 km]
4845) du (Dubai, United Arab Emirates) [3069.72 km]
4844) Etisalat (Dubai, United Arab Emirates) [3069.72 km]

Measure the Network Speed to Specific Server

You can manually specify server ID during testing, instead of using the geographically closest one.

Let’s test the link to “1746) Vodafone DE (Frankfurt, Germany) [1937.90 km]”:

$ speedtest --server 1746