How can the name of the most memory intensive application in usage be retrieved?

367 Views Asked by At

I need to retrieve the name of the most memory intensive application* on OS X. The solution should be in Objective-C while preferably refraining from using 3rd party APIs. The solution cannot contain any parsing.

*The memory intensity of an application can be defined as the amount of real memory in use by that application.

3

There are 3 best solutions below

2
On

Bash is pretty good for things like this. Using bash you can have the command:

ps -p `ps aux | awk '{print $4,$2}' | sort -nr | head -n 1 | awk '{print $2}'` | awk '{print $4}' | tail -n 1

(could probably be optimised, but it still works)

Then do something like:

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"ps -p `ps aux | awk '{print $4,$2}' | sort -nr | head -n 1 | awk '{print $2}'` | awk '{print $4}' | tail -n 1"];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"Command Returned:\n%@", string);

[string release];
[task release];

The bash command works, the objective c code is untested though.

2
On

you can use sysctl to retrieve the available processes. the SO question "Can we retrieve the applications currently running in iPhone and iPad?" has an answer that should works for macOS … i tried it, simply putting the code in the answer in an Xcode 4.4 new macOS project, #importing and performing an NSLog on the result array rather than returning it, and it neatly displays the collected array of process names and IDs.

and while there is a wealth of information in struct kinfo_proc and its nested struct extern_proc, unfortunately for you, i don't see an easy way to retrieve the memory information for individual processes.

for that, you can consult libtop.c, which is Apple's open source offering. the linked version is from the MacOS X 10.8 library.

in any case, if you combine pulling the available processes from sysctl with the process information retrieval code in libtop.c, you should end up with a programmatic solution for exactly what you're looking for.

and … on the other hand … if you don't mind doing a very small bit of parsing compared to the work this will require, try the SO answer You can use NSTask , only substitute ps aux -m where that question performs "grep". you'll want to get only the first real line of output from the stream, and you'll have to parse whitespace to get to the column containing the RSS information, but that might be easier than getting what you want via libtop.c , depending upon what you need it for.

2
On
ps aux -m

will give you a print out of the processess in terms of memory (highest memory at the top). If you access this by pipeing this command then filtering the top line.

Developer docs on NSPipe

once you have it piped in all you have to do is exstract the bits you want(NSMutableString).