Locating Largest Memory in Bash Script on Linux

Avatar

By squashlabs, Last Updated: Oct. 22, 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: Executing Configure Read from Bash Script 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

Related Article: Should You Use Numbers in Bash Script Names?

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.

Handling Pytest Failures in Bash Script on Linux

The article is a detailed walk-through that explains how to make a bash script fail when pytest fails in a Linux environment. The article provides st… read more

Exploring Local Scope of Bash Variables in Linux Scripts

Decoding the locality of bash variables in Linux scripting. This article explores the concept of local variables in bash scripting, their declaration… read more

Utilizing Variables from Bash Scripts in PHP on Linux

PHP developers often find themselves needing to access variables from bash scripts on Linux. This article explores the method for PHP to utilize thes… read more

How to Use Mkdir Only If a Directory Doesn't Exist in Linux

Creating a directory in Linux can be a simple task, but what if you only want to create it if it doesn't already exist? This article provides a step-… read more

How to SCP to a Folder in Linux: Copy Folder Remote<>Local

A simple guide for using scp to copy folders from remote to local in Linux. This article provides step-by-step instructions on how to establish a sec… read more

Comparing Array Sizes in Bash Scripting on Linux

Learn how to compare array sizes in bash scripting on Linux with a variety of code snippets and commands. Discover different approaches such as using… read more

How to Manipulate Quotes & Strings in Bash Scripts

A concise tutorial on manipulating quotes and strings in Bash scripts in a Linux environment. Learn how to add or remove quotes from strings, perform… read more

Fixing the 'Linux Username Not In The Sudoers File' Error

Resolving the 'Linux: is not in the sudoers file' issue can be frustrating, but there are solutions available. This guide provides step-by-step instr… read more

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 h… read more

How To Echo a Newline In Bash

Learn how to use the echo command in Bash to print a newline character in Linux. The article covers various methods, including using the echo command… read more