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

            評論

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

            久久精品无码一区二区三区| 久久久久久国产精品美女| 色老头网站久久网| 久久久久久国产a免费观看黄色大片| 亚洲精品国产第一综合99久久| 亚洲精品乱码久久久久久久久久久久| 色诱久久久久综合网ywww| 久久久久综合网久久| 久久国产精品波多野结衣AV| 欧美精品国产综合久久| 国产欧美久久一区二区| 热久久最新网站获取| 久久久久久久综合日本亚洲| 一本久久a久久精品综合香蕉 | 久久亚洲中文字幕精品一区| 久久久久免费看成人影片| 久久久精品一区二区三区| 久久久久久精品久久久久| 91亚洲国产成人久久精品| 久久精品国产男包| 久久免费香蕉视频| 久久福利青草精品资源站免费| 亚洲国产成人久久综合野外| 精品久久久久久亚洲| 亚洲国产精品18久久久久久| 亚洲成av人片不卡无码久久| 久久精品国产亚洲AV无码娇色| 色综合久久88色综合天天 | 久久综合九色综合精品| 久久精品无码专区免费青青| 免费精品久久久久久中文字幕 | 久久久久免费精品国产| 精品无码久久久久久午夜| 亚洲AV无码久久| 久久人人爽人人爽人人片AV东京热| 久久精品无码av| 欧美精品丝袜久久久中文字幕| 久久精品二区| 综合久久精品色| 久久这里只有精品首页| 亚洲精品无码久久久久|