views:

42

answers:

3

I'm running Mac OS 10.6. I want to run top to get memory usage, but not in interactive mode, or any mode that updates. I just want memory usage at that point in time then return to prompt. I've looked for other utilities to get memory usage... but came up short (vm_stat is for virtual memory). Can someone direct me how to get top or something else to print memory usage to stdout?

+1  A: 

top -l 1 will put just one sample to standard output (you can redirect it, filter it, etc, as you wish of course). man top for many more details.

Alex Martelli
Thanks. As I often do, I parse the entire man file and miss the only thing I need.
vgm64
top hangs for a half second between outputting and getting back to the prompt. Do you know why? It makes it hard to run this many times in a row.
vgm64
@vmg64, I think it's just the time it takes to collect all statistics (lots of syscalls, after all). If you need many samples, use `top -l 20` or whatever number you need!
Alex Martelli
@Alex I'm trying to get very quick calls. I'm mapping memory use over a few seconds while running programs. I'm going to ask a new question about it instead of asking just about top, but if you know how to speed it up, feel free to share.
vgm64
@vgm64, given your behavior wrt this answer (no upvote ever, despite the thanks you chose to express; acceptance later retracted) I'm unlikely to ever bother to answer any other question of yours again. Such behavior is just not very compatible with SO's workings.
Alex Martelli
@Alex I'm sorry I offended. I retracted the answer because I hadn't solved my problem, but you most definitely solved the original question. I've been very distracted this evening and did not give this post the attention it should have had. Is it common to upvote an answer when you select it as the accepted answer?
vgm64
@vgm, let me apologize to you in turn for showing myself a bit peeved -- I assure you it's not about "craving the rep" (I usually max out before bedtime anyway), it's about real care for SO's smooth working. Yes, one usually upvotes an answer if it's good on its own, then later accepts it if it's confirmed to have answered the question (sometimes the answer's so obviously resolutive that you upvote and immediately accept, but that's rare). Anyway, I'll be glad to help further if I can, and sorry again for the peevedness.
Alex Martelli
A: 

I've been using this command to spit out the basic info in the first few lines

top -l 1 -n 0

-l 1 = just one sample -n 0 = 0 processes

this is a bit of a hack .... but if you only want the memory line ... you could feed it through head and tail.

top -l 1 -n 0 | head -n 5 | tail -n 2
Andrew Neelands
+1  A: 

you can also use the ps command. eg

ps -eo pmem,comm

check the ps man page for more output formatting. eg rss, size etc..

ghostdog74