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.