Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fastest way to consume enough memory to cause the app to be dumped by the OS?

I have an app I call memory eater, which is meant to force other applications to be dumped by the os. It does this by consuming lots of memory over time until it is terminated due to memory pressure. In order to consume memory I basically make copies of JPEG representations of data:

-(IBAction)didTapStartButton:(id)sender{
    int i = 200;
    while (i>0) {
        NSData* data = [UIImagePNGRepresentation(self.image) mutableCopy] ;
        [self.array addObject:[[data description] mutableCopy]];
        [self.array addObject:data];
        i--;
    }
}

This was done entirely with trial and error, and I assume that there is a more straightforward way to consume lots and lots of memory.

like image 724
Saltymule Avatar asked Aug 14 '14 03:08

Saltymule


People also ask

How do you take a memory dump of an app?

Enable memory dump settingIn Control Panel, select System and Security > System. Select Advanced system settings, and then select the Advanced tab. In the Startup and Recovery area, select Settings. Make sure that Kernel memory dump or Complete memory dump is selected under Writing Debugging Information.

What is full memory dump?

A complete memory dump records all the contents of system memory when your computer stops unexpectedly. A complete memory dump may contain data from processes that were running when the memory dump was collected.


1 Answers

You can use malloc() in a loop.

while (1) {
    int *ptr = malloc(4096);
    assert(ptr != NULL);
    *ptr = 0;
}

The *ptr = 0 line is necessary to force the page to be dirty, otherwise you will consume address space instead of memory. The number 4096 ensures that each iteration through the loop will add exactly one dirty page to the address space, since 4096 is the most common page size.

like image 163
Dietrich Epp Avatar answered Nov 15 '22 00:11

Dietrich Epp