逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::

#import <Foundation/Foundation.h>

#import <stdio.h>


void memoryTestWithAutoreleasePool();

void memoryTestWithoutAutoreleasePool();

void memoryTestWithRelease();

void memoryTestWithoutRelease();


int main(int argc, char* argv[]) {

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    

    NSLog(@"Please input a number in [1, 2, 3, 4] to select method of memorytesting.");

    NSLog(@"1: memoryTestWithAutoreleasePool: No memory leak");

    NSLog(@"2: memoryTestWithoutAutoreleasePool: Memory leak");

    NSLog(@"3: memoryTestWithRelease: No memory leak");

    NSLog(@"4: memoryTestWithoutRelease: Memory leak");

    int n;

    scanf("%d", &n);

    void (*func)();

    switch(n) {

        case 1: func = memoryTestWithAutoreleasePool;

            break;

        case 2: func = memoryTestWithoutAutoreleasePool;

            break;

        case 3: func = memoryTestWithRelease;

            break;

        case 4: func = memoryTestWithoutRelease;

            break;

        default: NSLog(@"Unvalid number.");

            return 0;

    }

    

    fflush(stdin); // No definition by GCC.

    getchar();

    func();

    getchar();

    func();

    getchar();

    func();

    getchar();

    func();

    

    [pool drain];

    NSLog(@"--------Program is finished--------");

    return 0;

}


void memoryTestWithAutoreleasePool() {

    NSLog(@"...........Memory Test With AutoreleasePool Start...........");

    

    // pool放在方法中, 而不是由主函数的pool来管理.

 

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    NSMutableArray* array = [[NSMutableArray alloc] init];

    int length = 1000000;

    int i = 0;

    

    // Allocate memory.

    for (i = 0; i < length; ++i) {

        NSNumber* number = [NSNumber numberWithInt:i];

        [array addObject:number];

    }

    

    // Release memory.

    [array release];

    [pool drain];

    

    NSLog(@"...........Memory Test With AutoreleasePool End...........");

}


void memoryTestWithoutAutoreleasePool() {

    NSLog(@"...........Memory Test Without AutoreleasePool Start...........");

    NSMutableArray* array = [[NSMutableArray alloc] init];

    int length = 1000000;

    int i = 0;

    

    // Allocate memory.

    for (i = 0; i < length; ++i) {

        NSNumber* number = [NSNumber numberWithInt:i];

        [array addObject:number];

    }

    

    // Release memory.

    [array release];

    

    NSLog(@"...........Memory Test Without AutoreleasePool End...........");

}


void memoryTestWithRelease() {

    NSLog(@"...........Memory Test With Release Start...........");

    NSMutableArray* array = [[NSMutableArray alloc] init];

    int length = 1000000;

    int i = 0;

    

    // Allocate memory.

    for (i = 0; i < length; ++i) {

        NSNumber* number = [[NSNumber alloc] initWithInt:i];

        [array addObject:number];

    }

    

    // Release memory.

    for (i = 0; i < length; ++i) {

        NSNumber* number = [array objectAtIndex:i];

        [number release];

    }

    [array release];

    

    NSLog(@"...........Memory Test With Release End...........");

}


void memoryTestWithoutRelease() {

    NSLog(@"...........Memory Test Without Release Start...........");

    NSMutableArray* array = [[NSMutableArray alloc] init];

    int length = 1000000;

    int i = 0;

    

    // Allocate memory.

    for (i = 0; i < length; ++i) {

        NSNumber* number = [[NSNumber alloc] initWithInt:i];

        [array addObject:number];

    }

    

    // Release memory.

    [array release];

    

    NSLog(@"...........Memory Test Without Release End...........");

}








posted on 2008-10-02 17:31 逛奔的蜗牛 阅读(502) 评论(1)  编辑 收藏 引用 所属分类: C/C++

评论

# re: Cocoa内存管理测试 2008-10-02 17:36 暗金装备
第一次分配的20M空间即使在释放后还一直存在, 以后分配的20M空间可以被释放(即即使管理好了内存, 从始至终总会占用20M内存, 如果内存完全回收, 应该也只占用几百K). C++完美的把第一次分配的空间全部回收(整个程序只占用几百K).  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理