• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::

            #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 逛奔的蝸牛 閱讀(509) 評論(1)  編輯 收藏 引用 所屬分類: C/C++

            評論

            # re: Cocoa內存管理測試 2008-10-02 17:36 暗金裝備
            第一次分配的20M空間即使在釋放后還一直存在, 以后分配的20M空間可以被釋放(即即使管理好了內存, 從始至終總會占用20M內存, 如果內存完全回收, 應該也只占用幾百K). C++完美的把第一次分配的空間全部回收(整個程序只占用幾百K).  回復  更多評論
              

            久久久综合香蕉尹人综合网| 亚洲精品国精品久久99热一| AV色综合久久天堂AV色综合在| 国内精品久久人妻互换| 国产成人精品久久一区二区三区av| 久久99精品久久久久久齐齐| 久久久久久伊人高潮影院| 精品久久久久久| 精品久久久久成人码免费动漫| 精品久久久久久无码专区| 色欲综合久久躁天天躁| 国产精品免费福利久久| 一本一道久久a久久精品综合| 久久精品国产亚洲77777| 伊人久久精品影院| 一本久久a久久精品综合夜夜| 久久AV高潮AV无码AV| 中文精品久久久久国产网址| 2020久久精品亚洲热综合一本| 久久精品国产精品亚洲精品| 亚洲国产欧洲综合997久久| 久久男人AV资源网站| 亚洲一本综合久久| 国产精品久久一区二区三区| 亚洲成色999久久网站| 国产精品午夜久久| 人妻少妇久久中文字幕| 久久久不卡国产精品一区二区| 久久av无码专区亚洲av桃花岛| 久久综合亚洲色HEZYO社区| 久久精品这里只有精99品| 久久无码av三级| 97久久久久人妻精品专区| 无码AV中文字幕久久专区| 久久天天躁狠狠躁夜夜2020一| 久久综合九色综合欧美就去吻| 久久国产视屏| 久久久久久国产精品无码下载| 久久精品成人| 久久免费看黄a级毛片| 久久狠狠爱亚洲综合影院|