• <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 | 国产国产成人久久精品| www.久久热.com| 久久男人中文字幕资源站| 一级a性色生活片久久无| 九九久久自然熟的香蕉图片| 办公室久久精品| 久久久国产亚洲精品| 99久久国产综合精品麻豆| 日韩精品无码久久一区二区三| 国内精品伊人久久久久777| 亚洲国产天堂久久综合网站| 一本大道久久东京热无码AV| 国产婷婷成人久久Av免费高清| 久久亚洲AV无码西西人体| 欧美亚洲色综久久精品国产| 精品水蜜桃久久久久久久| 久久亚洲中文字幕精品有坂深雪| 99久久99久久精品国产| 国产毛片欧美毛片久久久| 99久久夜色精品国产网站| 久久99久久99精品免视看动漫| 伊人热人久久中文字幕| 亚洲狠狠婷婷综合久久久久| 内射无码专区久久亚洲| 99热成人精品免费久久| 久久国产高清字幕中文| 久久精品国产亚洲77777| 波多野结衣AV无码久久一区| 亚洲精品97久久中文字幕无码| 精品久久人人妻人人做精品| 久久久91精品国产一区二区三区| 新狼窝色AV性久久久久久| 国产精品久久久久久久人人看| 亚洲精品99久久久久中文字幕| 久久精品国产欧美日韩| 精品国产青草久久久久福利| 国产成人精品久久亚洲高清不卡| 久久精品国产99国产精偷| 狠狠干狠狠久久| 久久精品国产精品亚洲精品|