• <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>

            逛奔的蝸牛

            我不聰明,但我會(huì)很努力

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

            #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放在方法中, 而不是由主函數(shù)的pool來(lái)管理.

             

                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) 評(píng)論(1)  編輯 收藏 引用 所屬分類(lèi): C/C++

            評(píng)論

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

            一个色综合久久| 久久青青色综合| 精品久久香蕉国产线看观看亚洲| 国产精品免费福利久久| 丁香狠狠色婷婷久久综合| 久久国产精品一区二区| 久久久精品视频免费观看| 日韩精品久久久久久久电影| 久久精品国产亚洲av水果派 | 精品视频久久久久| 日本久久中文字幕| 777米奇久久最新地址| 91精品国产91热久久久久福利 | 欧美牲交A欧牲交aⅴ久久| 亚洲国产精品热久久| 亚洲狠狠婷婷综合久久久久| 9191精品国产免费久久| 欧洲成人午夜精品无码区久久| 99久久婷婷国产综合精品草原| 亚洲精品无码久久久久sm| 久久国产视频99电影| 国产精品久久久久久一区二区三区| 久久毛片免费看一区二区三区| 97久久超碰国产精品2021| 久久久亚洲欧洲日产国码是AV| 91久久精品国产成人久久| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | www.久久热.com| 久久久久精品国产亚洲AV无码| 免费一级做a爰片久久毛片潮| 91精品观看91久久久久久| 女人香蕉久久**毛片精品| 国产精品久久久久国产A级| 麻豆一区二区99久久久久| 亚洲精品tv久久久久久久久| 中文字幕乱码久久午夜| 18岁日韩内射颜射午夜久久成人| 伊人久久大香线蕉综合热线| 久久精品国产亚洲αv忘忧草| 国产成人综合久久精品红| 狠狠精品干练久久久无码中文字幕|