LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Calculate total memory consumed by user (https://www.linuxquestions.org/questions/linux-general-1/calculate-total-memory-consumed-by-user-833303/)

emymcse 09-20-2010 04:43 AM

Calculate total memory consumed by user
 
Hello,

is there a way to calculate the total memory consumed by user ?

Thanks

anurag2202 09-20-2010 04:49 AM

Solution
 
We can check it by using 'free' command.

quanta 09-20-2010 05:52 AM

Code:

# ps -eo size,pid,user,cmd | awk '$3 ~ /username/ { mem+=$1 } END { print mem }'

salasi 09-20-2010 02:03 PM

Quote:

Originally Posted by emymcse (Post 4103161)
is there a way to calculate the total memory consumed by user ?

You don't mean used (which I think was the intent behind post #2), and you do mean by 'a single user' (which I think was the intent behind post #3) and not 'all (ordinary?) users'.

The code in post #3 didn't work for me...which was a pity, as I thought it was a neat idea. Can you explain the awk part, as awk isn't something I normally use, please?

syg00 09-20-2010 04:48 PM

Change username to a valid user name, and try again.
Merely checks for that username and accumulates the first field (size). This is (probably) not what you would expect "size" to represent - see the manpage.

salasi 09-21-2010 04:08 AM

Quote:

Originally Posted by syg00 (Post 4103790)
Change username to a valid user name, and try again.

That's what I did, first time, and the outcome was a blank line. This is intended to work in bash, or some other shell?

quanta 09-21-2010 04:20 AM

@salasi: What shell are you using? Does 'ps -u <your_username>' show anything?

salasi 09-23-2010 03:43 AM

Quote:

Originally Posted by quanta (Post 4104262)
@salasi: What shell are you using? Does 'ps -u <your_username>' show anything?

(sorry about the delay): I was about to say bash, but it was really konsole 2.5 (kde 4.5.1), if that makes a difference. The output from ps -u is in the form:

Code:

3295 ?        00:00:00 gvfs-fuse-daemo
 3298 ?        00:02:33 knemo
 3302 ?        00:00:01 soffice.bin
 3303 ?        00:00:01 klipper
 3307 ?        00:00:00 gvfs-gdu-volume
 3312 ?        00:00:00 gvfs-gphoto2-vo
 3314 ?        00:00:54 kwalletd
 3316 ?        00:00:02 gvfs-afc-volume
 3321 pts/2    00:00:00 bash
 3331 ?        00:00:00 gconfd-2
 3338 ?        00:00:00 gconf-helper
 3356 ?        00:00:00 kwalletmanager
 5643 ?        00:00:11 kate
 5756 pts/0    00:00:00 bash
 8523 pts/4    00:00:00 bash
 8532 pts/5    00:00:00 bash

( 'random' extract from the middle of the output)

quanta 09-23-2010 04:02 AM

No, no. konsole is a terminal. The shell is: sh, bash, csh, ksh, ... You can check which shell you are using with 'echo $0', 'ps -p $$', ...

Could you please copy-paste when you run command in post #3?

quanta 09-23-2010 04:02 AM

No, no. konsole is a terminal. The shell is: sh, bash, csh, ksh, ... You can check which shell you are using with 'echo $0', 'ps -p $$', ...

Could you please copy-paste when you run command in post #3?

salasi 09-23-2010 04:37 AM

Quote:

You can check which shell you are using with 'echo $0', 'ps -p $$',
Code:

echo $0', 'ps -p $$
/bin/bash, ps -p 8523

Quote:

Originally Posted by quanta (Post 4106458)
No, no. konsole is a terminal. The shell is: sh, bash, csh, ksh, ... You can check which shell you are using with 'echo $0', 'ps -p $$', ...

Could you please copy-paste when you run command in post #3?

Code:

ps -eo size,pid,user,cmd | awk '$3 ~ /username/ { mem+=$1 } END { print mem }'
..now can you start answering some questions...

quanta 09-23-2010 04:46 AM

Did you change username to your login name?

jschiwal 09-23-2010 05:26 AM

Look at output of the first part on it's own.
ps -eo size,pid,user,cmd

Since you just need the size and user, how about:
ps -eo size,user

Sometimes when an output on the left hand side of a pipe is blank, it's because of an error on the right hand side, such as a missing or aliased command.

Also, what does "ps --version" return? I've seen similar one-liners using ls & awk that didn't work out right when I tried them because of a small difference in the output such as how recent dates were printed.


All times are GMT -5. The time now is 12:35 PM.