• <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來管理.

             

                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 逛奔的蝸牛 閱讀(512) 評(píng)論(1)  編輯 收藏 引用 所屬分類: 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)論
              

            伊人久久综合成人网| 久久99精品久久久久久齐齐| 久久伊人精品一区二区三区| 亚洲色大成网站www久久九| 日本精品久久久久影院日本| 久久福利资源国产精品999| 精品久久久久久无码专区不卡| 久久精品水蜜桃av综合天堂| 91久久香蕉国产熟女线看| 久久久久亚洲爆乳少妇无 | 久久国产成人午夜AV影院| 色综合久久88色综合天天 | 国产成人精品白浆久久69 | 久久精品国产一区二区电影| 精品久久亚洲中文无码| 国产精品VIDEOSSEX久久发布| 久久精品国产亚洲AV大全| 国产精品嫩草影院久久| 狠狠色丁香婷综合久久| 久久精品国产亚洲αv忘忧草| 久久久久18| 国产精品激情综合久久| 久久超乳爆乳中文字幕| 久久天天躁狠狠躁夜夜不卡| 国产午夜久久影院| 国产三级久久久精品麻豆三级| A级毛片无码久久精品免费| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 国产精品久久网| 久久久av波多野一区二区| 精品综合久久久久久97| 亚洲性久久久影院| 久久综合九色综合久99| 久久久久久毛片免费看| 久久亚洲国产成人影院网站 | 久久精品一区二区国产| 少妇久久久久久被弄高潮| 亚洲中文字幕无码一久久区| 伊人热热久久原色播放www| 中文字幕无码av激情不卡久久| 噜噜噜色噜噜噜久久|