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.
you can use
sysctlto 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_procand its nestedstruct 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 -mwhere 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.