Shardul
Shardul

Reputation: 4274

Mac OSX - Get all running application list with memory usage

I am very new to develop mac osx application using xcode. I am trying to get all running application list with their memory usage. Can any body help me in this case.

Please help me.

Thanks in advance.

Upvotes: 2

Views: 1834

Answers (2)

Neha
Neha

Reputation: 1751

You may get the cpu usage as:

- (NSString*) get_process_usage:(int) pid 
{
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath:@"/bin/ps"];

     NSArray *arguments;
     arguments = [NSArray arrayWithObjects: @"-O",@"%cpu",@"-p",[NSString stringWithFormat:@"%d",pid], nil];
     [task setArguments: arguments];

     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];

     NSString* temp = [string stringByReplacingOccurrencesOfString:@"PID  %CPU   TT  STAT      TIME COMMAND" withString:@""];

     NSMutableArray* arr =(NSMutableArray*) [temp componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
     [arr removeObject:@""];

     [string release];
     [task release];

     if([arr count]>=2)
         return [arr objectAtIndex:1];

     else
        return @"unknown";

 }

Upvotes: 2

Dhanashree
Dhanashree

Reputation: 79

It will help you to get list of running application :

for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications]) {
        NSLog(@"%@",[app localizedName]);
    }

Upvotes: 2

Related Questions