How to make an iPhone app that quickly fills memory

I often try to fix errors that occur when I use my iphone for other puzzles, and he needs to free up some memory and thus unload some views from my application. It was quite difficult for me to imitate when I needed it, so I decided to do this, trying to allocate as much memory as possible and force my tested application to free unused views, etc.

I tried something simple, like calling it every few hundred milliseconds, but for some reason I didn’t do anything

[[NSData alloc] initWithBytes:malloc(2048 * 1024) length:2048 * 1024]; 

The tools show that applications are becoming more and more, far exceeding the amount of iphone memory (hundreds of allocated mbs), but I do not even receive a memory warning and generally do not affect other applications. Is there some kind of protection that somehow prevents the iphone application form creation? Or is there some kind of error in my assumptions about how the iphone works? How do you solve this problem when you come across it?

EDIT: I run my application on the device, I could not parse my views on the simulator, even if I modeled a warning about memory (this sometimes works, but only rarely)

EDIT2: since the problem with bbum was really in virtual distribution, a simple memset after the allocation did the trick

 void *data = malloc(1024 * 1024); memset(data, 0, 1024 * 1024); 
+6
memory-management iphone
source share
5 answers

Most likely, what is happening is a dodgy bit of virtual addressing behind your back.

Namely, the application can reserve up to 4 GB of the 32-bit address space available to it for free (less than 4 GB, indeed, because there is fragmentation caused by various elements of the system).

When you execute malloc(REALLYBIGNUMBER) , the system uses vm_allocate() to execute the request. The mask memory manager transfers addresses to you, but does not actually return them with real memory (it seems like the US economy has tons of $$$ in the market that are not backed up by any real asset).

Physical memory will only be used when you write (or, technically, read) something in memory. And this will happen only on the pages.

So, if you need to go through a dedicated buffer in increments of 4096 (page size) and write one byte, you would quickly see what real memory is being consumed.

+4
source share

Can you not just simulate a memory warning in the simulator using the Device menu?

+4
source share

Here is what I do to simulate the use of a large block of memory.

 #define MEGA (1024 * 1024) - (void)stressTestTheApplicationBySimulatingMemoryScarcity { NSUInteger sizeInMB = 20; // Size in MB. The higher, the more memory will be used here, leaving less for the application // On iPad: 30 seems to be the upper limit you can ask. YMMV #if MEMORY_STRESS_TEST_SCARCITY_ENABLED #warning MEMORY_STRESS_TEST_SCARCITY_ENABLED - THIS SHOULD NOT HAPPEN IN PRODUCTION #else return; #endif NSLog(@"MEMORY STRESS TEST ENABLED (%dMB)", sizeInMB); void * unusedMemoryBufferToSimulateMemoryScarcity = NSZoneCalloc(NSDefaultMallocZone(), sizeInMB * MEGA, 1); if (NULL == unusedMemoryBufferToSimulateMemoryScarcity) { NSLog(@"MEMORY STRESS TEST FAILED: Was unable to allocate requested memory"); return; } dataToRetainToSimulateMemoryScarcity = [[NSData dataWithBytesNoCopy:unusedMemoryBufferToSimulateMemoryScarcity length:sizeInMB * MEGA freeWhenDone:YES] retain]; if (dataToRetainToSimulateMemoryScarcity == nil) { NSLog(@"Failed to retain data to simulate memory scarcity"); } } 

This is done, the allocation of memory is visible using tools.
The memset solution proposed by bbum may have done the trick instead of dataWithBytesNoCopy: length: freeWhenDone:

A warning and logs left in place help make sure that I do not send them by mistake.
If you add some of this code to the project, I also suggest that you save it (and enable the "Warning as an error" option).

+2
source share

You need to bzero (or write to each page differently) the memory blocks after allocating them, so that the virtual memory system creates all the pages in the block and marks them resident + dirty.

+1
source share

Do you use tools in a process running on your device or in a simulator?
I think that in the simulator it uses your Mac memory, which is clearly larger than that of the iPhone. Try running it on your device and see what happens?

Disco

0
source share

All Articles