title: Find out which processes use the most memory and kill them
body: <p>I currently have a bash script that checks my machine memory state and sends me an email alert whenever its memory exceeds some threshold, my question here is:</p>
  
  <ul>
  <li>Is there a way to find out which processes are consuming the most memory on the machine and kill them within the script ?</li>
  </ul>
  
  <p>Here is my script, as it is for now:</p>
  
  <pre><code>#!/bin/bash
  
  ############################################################
  #                  Memory usage function                   #
  #           Captures memory usage in percentage            #
  #  Sends an email alert if memory usage exceeds threshold  #
  ############################################################
  
  memory_check() {
      total_ram=`cat /proc/meminfo | grep 'MemTotal' | awk '{print $2}'`;
      free_ram=`cat /proc/meminfo | grep 'MemFree' | awk '{print $2}'`;
      used_ram=$(($total_ram - $free_ram))
      mem_percent=$(($used_ram * 100 / $total_ram))
      machine_name=`hostname`
      threshold=95
      if [ $mem_percent -gt $threshold ]; then
          echo "Memory usage has exceeded $threshold% threshold and was at $mem_percent%." &gt; memory_alert_report
          mailx -s "Memory resource alarm on $machine_name !" my@adress.com &lt; memory_alert_report
      else
          exit;
      fi
      exit;
  }
  </code></pre>