• <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).  回復  更多評論
              

            国内精品久久久久久久亚洲| 精品多毛少妇人妻AV免费久久| 一级做a爰片久久毛片16| 国内精品久久久久久麻豆| 精品久久久久成人码免费动漫| 久久久亚洲欧洲日产国码二区| 久久黄视频| 国产三级久久久精品麻豆三级 | 一级A毛片免费观看久久精品| 久久精品国产亚洲AV香蕉| 2021国产成人精品久久| 亚洲午夜久久久久久噜噜噜| 国产精品综合久久第一页| 久久中文骚妇内射| 欧美成人免费观看久久| 狠狠久久综合| 亚洲国产精品婷婷久久| 97久久婷婷五月综合色d啪蜜芽| 亚洲综合久久综合激情久久| 婷婷综合久久中文字幕蜜桃三电影 | 亚洲精品tv久久久久久久久久| 国内精品久久久久久99| 99久久精品免费看国产一区二区三区| 久久精品国产福利国产琪琪| 久久亚洲国产午夜精品理论片| 久久久无码人妻精品无码| 无码国内精品久久人妻蜜桃| 久久经典免费视频| 久久亚洲精品国产精品婷婷| 亚洲精品无码久久不卡| 久久久久一级精品亚洲国产成人综合AV区| 国内精品久久久久影院日本| 久久久久女人精品毛片| 日韩久久久久久中文人妻| 无码人妻精品一区二区三区久久| 久久久久精品国产亚洲AV无码 | 99精品国产在热久久| 99久久国语露脸精品国产| 国产99精品久久| 色综合久久久久| 久久久久久青草大香综合精品|