1

I am trying to fetch memory usage with below top command.

KiB Mem :  8009480 total,  1438848 free,  1964392 used, 4606240 buff/cache 
KiB Swap:  7340028 total,  5302364 free,  2037664 used. 5202692 avail Mem
top -p $PID -n 1 -b | grep 'KiB Mem :' | awk -F, '{print $3}'
1963780 used

Strangely sometimes I receive the output as

*"15108960+used"*

I am not able to understand why are the +,* symbol coming in between sometimes.

I couldn't find anything in man page about it.

What does that mean? How do I make top command print without * and + in the output.

Note : Running in CentOs Machine.

3
  • 2
    Why not getting the info directly from /proc/meminfo? Commented Jan 27, 2022 at 7:52
  • Regarding the * sign, could you please paste here a full line where you see it, without grep and awk? Since I've never seen it in top, I'm curious to see an example. You can just copy to the paste the entire Summary area of top where you see *, including the Cpu and the Swap lines.
    – aviro
    Commented Jan 27, 2022 at 13:13
  • @StéphaneChazelas: I need it for a particular pid. Commented Feb 3, 2022 at 5:12

1 Answer 1

6

Firstly, top is not a good way to get the memory information of the machine. The free command is meant to be used to get memory information. You could also use vmstat -s.

Now, regarding top, I've never seen * character in the Summary Area, but explanation of the + signs appears in the man pages of top(1):

          If you see a `+' between a displayed number and the
          following label, it means that top was forced to truncate
          some portion of that number.  By raising the scaling
          factor, such truncation can be avoided.

You can change the Scaling from KiB to something else using the -E flag or the E interactive command.

   -E  :Enforce-Summary-Memory-Scaling as:  -E  k | m | g | t | p | e
        Instructs top to force summary area memory to be scaled as:
           k - kibibytes
           m - mebibytes
           g - gibibytes
           t - tebibytes
           p - pebibytes
           e - exbibytes

        Later this can be changed with the `E' command toggle.

So, for instance, -Em will change the scaling from KiB to MiB.

But again, if you just want to get the memory information, don't use top, use free.

4
  • How can I use free command for a particular pid like the top command I used above? Commented Feb 3, 2022 at 4:58
  • @InQusitive, your top command didn't actually get the memory for a specific pid. I mean, it was, but then your grep/awk were only showing the system general memory usage, not the pid's memory usage. I'll add to my answer next week, but for now know you can use ps (for instace: ps -o rss -p <pid>) or pidstat (for instance: pidstar -r -p <pid>). Read their respective man pages for more information. You can also find memory stats in /proc/[pid]/status.
    – aviro
    Commented Feb 3, 2022 at 16:10
  • @InQusitive actually, it's better you open a new question on how to check process's memory usage in Linux, and show what field from top command you want to show, since this is a different question. Again, just remember that your current full command shows the system's general usage and not the process', so instead of using the command with grep/awk, just show the full top output and say which field you want to easily get for the process.
    – aviro
    Commented Feb 3, 2022 at 16:15
  • I used the /proc/[pid]/status and it's working good. Commented Feb 4, 2022 at 11:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .