Locating Largest Memory in Bash Script on Linux


By squashlabs, Last Updated: October 21, 2023

Locating Largest Memory in Bash Script on Linux

Checking Memory Usage in Linux

To check memory usage in Linux, there are several commands and tools available that provide detailed information about memory utilization. Let’s explore some of the commonly used commands in this section.

1. free

The free command is used to display information about the total, used, and free memory on a Linux system. It also provides information about swap memory usage if enabled. Here’s an example of using the free command:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           7.7G        3.1G        2.0G        127M        2.6G        4.2G
Swap:          2.0G        0B          2.0G

In the output, the “total” column represents the total amount of physical memory, the “used” column represents the memory currently in use, the “free” column represents the available free memory, and the “shared” column represents memory used by shared libraries. The “buff/cache” column represents the memory used by the kernel for caching file system data, and the “available” column represents the memory available for new processes without swapping.

2. top

The top command is a useful command-line utility that provides real-time information about system processes, including memory usage. To view memory usage in top, press the “Shift + M” keys. This will sort the processes based on their memory usage, with the highest memory-consuming processes listed at the top.

$ top
top - 23:47:52 up 1 day,  5:17,  2 users,  load average: 0.00, 0.01, 0.05
Tasks: 183 total,   1 running, 182 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7916.1 total,   2097.1 free,   3279.1 used,   2540.0 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   4043.0 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   1325 root      20   0   11.8g   1.2g   1.3g S   0.0  15.8   0:10.04 java
   1631 root      20   0  620.8m  90.1m  66.0m S   0.0   1.1   0:01.04 gnome-shell
   1460 root      20   0  755.6m  80.5m  64.0m S   0.0   1.0   0:02.09 Xorg
   1967 john      20   0 3018.2m  50.1m  36.2m S   0.0   0.6   0:01.15 chrome

In the output, the “%MEM” column represents the percentage of memory used by each process. Sorting the processes based on this column helps identify the processes consuming the most memory.

3. ps

The ps command is used to display information about active processes on a Linux system. The -o option can be used to customize the output format. To view memory usage with ps, use the following command:

$ ps -eo pid,cmd,%mem --sort=-%mem
  PID CMD                         %MEM
 1325 java                        15.8
 1631 gnome-shell                  1.1
 1460 Xorg                         1.0
 1967 chrome                       0.6

In the output, the “%MEM” column represents the percentage of memory used by each process. The --sort=-%mem option sorts the processes based on memory usage, with the highest memory-consuming processes listed first.

These are just a few examples of commands that can be used to check memory usage in Linux. There are many more tools and commands available, each with its own set of features and options.

Related Article: Adding Color to Bash Scripts in Linux

Memory Utilization in Linux

In Linux, memory utilization refers to the way the operating system manages and allocates memory resources. Memory is a critical aspect of system performance, and understanding how it is utilized can help optimize resource allocation and improve overall system efficiency.

Linux uses a combination of physical and virtual memory to manage processes and data. Physical memory, also known as RAM (Random Access Memory), is the actual hardware component that stores data and instructions that are actively being used by the system and applications. Virtual memory, on the other hand, is a technique that allows the operating system to use disk space as an extension of physical memory when the available RAM is insufficient to accommodate all the running processes.

Memory Allocation in Linux

Memory allocation in Linux refers to the process of assigning memory resources to different applications and processes running on the system. The Linux kernel manages memory allocation using various algorithms and techniques to ensure efficient usage of available resources.

One of the key mechanisms used for memory allocation in Linux is the “buddy system” algorithm. This algorithm divides the available memory into fixed-size blocks and maintains a linked list of free memory blocks of each size. When a process requests memory, the kernel searches for a suitable free block based on the requested size. If a block of the exact size is not available, the kernel looks for the next larger block and splits it into smaller blocks until the desired size is reached. This ensures efficient memory allocation by minimizing fragmentation.

Another important concept related to memory allocation is the concept of memory pages. In Linux, memory is divided into fixed-size pages, typically 4KB in size. These pages are the basic units of memory allocation and are used by the kernel to manage memory resources. Processes are allocated memory in multiples of these pages, and the kernel keeps track of the allocated pages for each process.

Related Article: How to Calculate the Sum of Inputs in Bash Scripts

Terminate Bash Script Loop via Keyboard Interrupt in Linux

Learn how to end a bash script loop using a keyboard interrupt in a Linux environment. Discover the keyboard interrupt signal in Linux and find out how to stop a loop in... read more

How to Handle Quotes with Md5sum in Bash Scripts

When working with md5sum bash scripts in Linux, it is important to understand how to handle quotes. This article provides a thorough look at the best practices for... read more

Accessing Seconds Since Epoch in Bash Scripts

Detailed instructions on how to access seconds since epoch in bash scripts in a Linux environment. Learn how to convert epoch time to a readable date format, get the... read more

Preventing Terminal Print from Bash Scripts in Linux

Learn how to prevent bash scripts from printing to the terminal in Linux. Discover various methods to redirect, suppress, or disable printing in your scripts. From... read more

Displaying Memory Usage in Bash Scripts on Linux

Learn how to display memory usage within a bash script on a Linux environment. This article covers various commands such as top, vmstat, sar, and ps, along with... read more

Parent Variable Accessibility in Bash Scripts

This tutorial offers an in-depth exploration of how a bash script in Linux can access its parent's variables. The article covers topics such as accessing and modifying... read more