如何以编程方式清理RAM可可?

时间:2015-11-02 12:07:14

标签: objective-c cocoa

在RAM中,有四种类型(有线存储器,活动存储器,非活动存储器,空闲存储器)

我已经获取了这四种类型内存的值。但我想通过清除RAM。如何在objective-c中以编程方式清除RAM?

以下是一些代码:

#import <sys/sysctl.h>
#import <mach/host_info.h>
#import <mach/mach_host.h>
#import <mach/task_info.h>
#import <mach/task.h>
#import "AppDelegate.h"




@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSView *myview ,*view2;

@end

@implementation AppDelegate
@synthesize myview;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    int mib[6];
    mib[0] = CTL_HW;
    mib[1] = HW_PAGESIZE;

    int pagesize;
    size_t length;
    length = sizeof (pagesize);
    if (sysctl (mib, 2, &pagesize, &length, NULL, 0) < 0)
    {
        fprintf (stderr, "getting page size");
    }

    mach_msg_type_number_t count = HOST_VM_INFO_COUNT;

    vm_statistics_data_t vmstat;
    if (host_statistics (mach_host_self (), HOST_VM_INFO, (host_info_t) &vmstat, &count) != KERN_SUCCESS)
    {
        fprintf (stderr, "Failed to get VM statistics.");
    }

//    double total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
  //  double wired = vmstat.wire_count / total;
  //  double active = vmstat.active_count / total;
  //  double inactive = vmstat.inactive_count / total;
  //  double free = vmstat.free_count / total;

    task_basic_info_64_data_t info;
    unsigned size = sizeof (info);
    task_info (mach_task_self (), TASK_BASIC_INFO_64, (task_info_t) &info, &size);

    double unit = 1024 * 1024;
  //
  //  NSString *resultstring = [NSString stringWithFormat: @"% 3.1f MB\n% 3.1f MB\n% 3.1f MB", vmstat.free_count * pagesize / unit, (vmstat.free_count + vmstat.inactive_count) * pagesize / unit, info.resident_size / unit];    NSLog(@"%fGB",total);


    NSLog(@"Free :% 3.1fMB",vmstat.free_count * pagesize / unit);
    NSLog(@"wired :% 3.1fMB",vmstat.wire_count * pagesize / unit);
    NSLog(@"active :% 3.1fMB",vmstat.active_count * pagesize / unit);
    NSLog(@"inactive :% 3.1fMB",vmstat.inactive_count * pagesize / unit);
}

1 个答案:

答案 0 :(得分:0)

实现像内存清理器这样的应用程序:只需分配尽可能多的内存。其他应用程序将获得内存压力通知,OS X将尝试为您的应用程序免费提供。然后释放刚刚分配的内存,留下空闲内存。