Memory usage

4 snippets across 3 stacks - Bash & Linux, Docker, Interview Prep

SHBash & Linux

Interactive Process Viewer

SH · Process Management
Syntax
top
htop
Example
top -o %MEM
htop -u deploy

Note top is always available. In top: press M to sort by memory, P by CPU, k to kill a process, q to quit. htop is a friendlier alternative with mouse support, color, and tree view but may need installing. -u filters by user.

Memory Usage

SH · System Info
Syntax
free [options]
Example
free -h
Output
              total   used   free   shared  buff/cache  available
Mem:          16Gi   5.2Gi  2.1Gi  312Mi    8.7Gi       10Gi
Swap:         4.0Gi  128Mi  3.9Gi

Note -h for human-readable. The 'available' column is the best indicator of how much memory is actually free for applications (it includes reclaimable buffer/cache). Linux aggressively caches, so low 'free' is normal and does not mean you are out of memory. Not available on macOS; use vm_stat instead.

DKDocker

Live Resource Usage

DK · Containers
Syntax
docker stats [container...]
Example
docker stats
docker stats web-api db-postgres --no-stream
Output
CONTAINER   CPU %   MEM USAGE / LIMIT     NET I/O         BLOCK I/O
web-api     0.50%   85.2MiB / 512MiB      1.2kB / 648B    0B / 4.1kB

Note Without arguments, it shows all running containers. Add --no-stream to get a single snapshot instead of a live-updating display.

DSAInterview Prep

Space Complexity

DSA · Big-O Notation
Syntax
Measure extra memory used beyond the input.
- Variables / pointers: O(1)
- New array of size n: O(n)
- Recursive call stack of depth n: O(n)
- 2D matrix n×m: O(n*m)
Example
// JavaScript
// O(1) space - in-place swap
function reverseInPlace(arr) {
  let l = 0, r = arr.length - 1;
  while (l < r) {
    [arr[l], arr[r]] = [arr[r], arr[l]];
    l++; r--;
  }
}

// O(n) space - new array
function reversed(arr) {
  return arr.slice().reverse();
}

# Python
def reverse_in_place(arr):
    l, r = 0, len(arr) - 1
    while l < r:
        arr[l], arr[r] = arr[r], arr[l]
        l += 1; r -= 1

def reversed_copy(arr):
    return arr[::-1]  # O(n) space
Output
In-place: O(1) space | Copy: O(n) space

Note Interviewers often ask 'Can you do it in O(1) space?' - this means modify the input in place. Recursive solutions use O(depth) stack space even if no extra data structures are created. Always mention auxiliary space vs total space.

Frequently asked questions

How does Bash & Linux handle memory usage?
This task is covered in 3 stacks on this page: Bash & Linux, Docker, Interview Prep. The "Interactive Process Viewer" snippet in Bash & Linux uses `top`.
Which command does the Bash & Linux example use?
The "Interactive Process Viewer" snippet uses `top`, from the Process Management section of the Bash & Linux cheat sheet.
Which stacks cover "memory usage" on this page?
Bash & Linux, Docker, Interview Prep. Together they hold 4 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Interactive Process Viewer": top is always available. In top: press M to sort by memory, P by CPU, k to kill a process, q to quit. htop is a friendlier alternative with mouse support, color, and tree view but may need installing. -u filters by user.