On Linux, we can cat the file “/proc/meminfo” to display memory related information:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | $ cat /proc/memoinfo MemTotal: 32939372 kB MemFree: 31155452 kB MemAvailable: 31269760 kB Buffers: 242964 kB Cached: 200756 kB SwapCached: 10472 kB Active: 853936 kB Inactive: 630008 kB Active(anon): 569276 kB Inactive(anon): 467960 kB Active(file): 284660 kB Inactive(file): 162048 kB Unevictable: 0 kB Mlocked: 0 kB SwapTotal: 1048572 kB SwapFree: 829436 kB Dirty: 24 kB Writeback: 0 kB AnonPages: 1037860 kB Mapped: 91908 kB Shmem: 376 kB Slab: 176904 kB SReclaimable: 75392 kB SUnreclaim: 101512 kB KernelStack: 7360 kB PageTables: 20676 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 17518256 kB Committed_AS: 3736228 kB VmallocTotal: 34359738367 kB VmallocUsed: 0 kB VmallocChunk: 0 kB HardwareCorrupted: 0 kB AnonHugePages: 0 kB ShmemHugePages: 0 kB ShmemPmdMapped: 0 kB CmaTotal: 0 kB CmaFree: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB DirectMap4k: 812896 kB DirectMap2M: 32741376 kB DirectMap1G: 2097152 kB |
$ cat /proc/memoinfo MemTotal: 32939372 kB MemFree: 31155452 kB MemAvailable: 31269760 kB Buffers: 242964 kB Cached: 200756 kB SwapCached: 10472 kB Active: 853936 kB Inactive: 630008 kB Active(anon): 569276 kB Inactive(anon): 467960 kB Active(file): 284660 kB Inactive(file): 162048 kB Unevictable: 0 kB Mlocked: 0 kB SwapTotal: 1048572 kB SwapFree: 829436 kB Dirty: 24 kB Writeback: 0 kB AnonPages: 1037860 kB Mapped: 91908 kB Shmem: 376 kB Slab: 176904 kB SReclaimable: 75392 kB SUnreclaim: 101512 kB KernelStack: 7360 kB PageTables: 20676 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 17518256 kB Committed_AS: 3736228 kB VmallocTotal: 34359738367 kB VmallocUsed: 0 kB VmallocChunk: 0 kB HardwareCorrupted: 0 kB AnonHugePages: 0 kB ShmemHugePages: 0 kB ShmemPmdMapped: 0 kB CmaTotal: 0 kB CmaFree: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB DirectMap4k: 812896 kB DirectMap2M: 32741376 kB DirectMap1G: 2097152 kB
We can grep a keyword and print the value using awk text filter. For example, the following BASH function allows you to extract the memory information with a key:
1 2 3 4 5 6 7 8 9 | #!/bin/bash function get_mem_info () { local key="$1" local val=$(cat /proc/meminfo | grep "$key" | awk -F ' ' '{ print $2; }') echo $val } get_mem_info $1 |
#!/bin/bash function get_mem_info () { local key="$1" local val=$(cat /proc/meminfo | grep "$key" | awk -F ' ' '{ print $2; }') echo $val } get_mem_info $1
For example:
1 2 | $ ./get_mem_info MemTotal 32939372 |
$ ./get_mem_info MemTotal 32939372
BASH Programming/Shell
- Three Interesting/Fun BASH Commands
- One Interesting Linux Command (Steam Locomotive in BASH)
- Simple Bash Function to Repeat a Command N times with Retries
- How to Extract a Domain Name from a Full URL in BASH?
- BASH Function to Compute the Greatest Common Divisor and Least Common Multiples
- BASH Function to Get Memory Information via AWK
- BASH Function to Escape Parameters Argument
- BASH Function to Check if sudo Available
- Full Permutation Algorithm Implementation in BASH
- BASH Function to Install Docker
- A Simple BASH Function/Script to Run a Command in Background
- BASH Script to Compute the Average Ping to a Domain
- A Bash Process Controller to Start, Stop and Restart an Daemon Program
- Linux BASH shell - Echo This if you get angry
- Bash SHELL, Chess Board Printing
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
268 wordsloading...
Last Post: Teaching Kids Programming - Concatenation of Arrays
Next Post: Teaching Kids Programming - Delete Node in a Linked List (No access to Head)