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.
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.
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 swapfunctionreverseInPlace(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 arrayfunctionreversed(arr){return arr.slice().reverse();}
# Python
def reverse_in_place(arr):
l, r =0,len(arr)-1while 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.