Roger Gilbrat
Roger Gilbrat

Reputation: 3845

Forcing iOS to run out of memory

I have spent the last 4 hours trying to get my iPad to run out of memory and I just can't make it happen.

Oh, the irony.

I want to testing some logging code, and I need to App the be killed for using too much memory but I can't make it happen, no matter how much memory I alloc. I am using the following code to stress it out:

if (tempArray == nil)
    tempArray = [NSMutableArray array];

NSData *data = [NSData dataWithBytes:malloc(10000000) length:10000000];

[tempArray addObject:data];

I run this every frame (30 times a second). Eventually malloc just returns NULL, but I don't ever see memory warning like my testers are seeing. tempArray is getting the NSData objects added to it.

I am running this on the device. Is there a sure-fire way to get your App to use too much memory and be killed?

Upvotes: 4

Views: 1902

Answers (3)

Theo
Theo

Reputation: 4015

To increase memory pressure in Swift 5, you can create large Data instances. For example, this method wastes around 500 MB on each call:

static var wastedMemory: Data = Data()

static func waste() {
  let data = Data(repeating: 0, count: 500_000_000)
  wastedMemory.append(data)
}

Upvotes: 1

Snips
Snips

Reputation: 6773

You can simulate a low memory situation when using the simulator, the option can be found under the iOS Simulator 'Hardware' menu.

Upvotes: 2

sch
sch

Reputation: 27536

Try loading a large image multiple times and never release it, the same way you currently do with NSData.

Upvotes: 2

Related Questions