Search code examples
cocoa-touchmemory-managementinstruments

Simulate Memory Warning While Running Instruments?


Is there a way to do this? If so, how, and can it be done while running an app in the simulator and/or the real device?

Is there an alternative way to test response to memory warnings in Instruments?


Solution

  • On the device, as far as I know, the only way to have a memory warning is actually creating a low memory condition. You can do that by allocating a big chunk of memory and freeing it after a few seconds (don't forget to release it, anyway). This may appear like a hack, but is the most reliable way of producing a low memory condition.

    About the simulator, as you possibly know, using Instruments to check for memory/performance issues while running your app within the simulator is not entirely reliable. Anyway, if you would like to do that, you can try sending this notification:

    - (void)simulateMemoryWarning
    {
    #if TARGET_IPHONE_SIMULATOR
      #ifdef DEBUG
        CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),   (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);
      #endif
     #endif
    }
    

    (Source)

    Remember also that the simulator has got the "Hardware/Simulate Memory Warning" command.