Page 1 of 1

Memory hog experiment

PostPosted: Tue Jun 02, 2015 10:54 pm
by Ayr3s
I wanted to crash/make a system unusable by using a lot of memory. I set up a virtual machine running linux mint rebecca and started testing some code, but I didn't get the results I expected. So I asked about it in #coffeesh0p and got nice info about that from MadMouse and Zylopfa and I decided to try it again.

I searched the web for more info and found this website:
http://www.linuxdevcenter.com/pub/a/linux/2006/11/30/linux-out-of-memory.html?page=1
wich has good info about the subject and some code!
I tried the examples from the website and modified the second one slightly to make my memory hog:

Code: Select all
#include <stdlib.h>
#include <stdio.h>

int main() {
void *block = NULL;
int count = 0;
while(1){
block = malloc(1024*1024);
if (!block) fork();
else memset(block, 1, 1024*1024);
fork();
printf("%d MB\n", ++count);
}
return 0;
}


Bad joke warning!
I call it brucewillis.c because it is hard to kill. :lol:
It does what I wanted and every time I tried running it in my virtual machine it got so slow that I had to kill the vm and start it again. I tried it with 512Mb and 2048Mb of RAM (no swap) and both freezed very quickly after starting the program.

The thing is, for now I didn't have the time to do much more than that, but I want to understand better how linux manages memory, so I'll be trying more stuff as soon as I have the time to do so and I'll update here with new info. :)

Re: Memory hog experiment

PostPosted: Wed Jun 03, 2015 1:32 am
by ghost107
I think your VM got slower because of the extensive CPU usage:
Your code doesn't contain only allocating memory, it also contain an infinite loop, and a fork, which forks your process.

Re: Memory hog experiment

PostPosted: Wed Jun 03, 2015 2:47 pm
by Ayr3s
Indeed, when I run the program virtualbox just slams to the execution cap. I couldn't see that in htop inside the vm because htop also freezes and stops updating, but I can see how much cpu it's using from the outside. Thanks for pointing that out! :D

Re: Memory hog experiment

PostPosted: Thu Jun 04, 2015 6:56 am
by Iblist
Looks like a fork bomb that also mallocs a bunch of memory while creating child processes.

Try running this in your virtual machine.

Code: Select all
int main()
{
    while (1) fork();
}


It's basically the same thing, but without mallocing any memory.

The way Linux handles memory, to my limited knowledge, is using process IDs. When a program, this fork bomb for example, is run memory is allocated and a process ID assigned to that specific process. A fork bomb is just a program that makes a copy of itself, eventually using up all the system's memory.

Fork bombs are my favourite payload, because they're very noticeable when they work, and they don't (read shouldn't) damage the victims pc. And since they're so simple to write, if I ever see an unlocked laptop with an Internet Explorer icon on it...well, that icon won't be running Internet Explorer for much longer.

Re: Memory hog experiment

PostPosted: Fri Jun 12, 2015 10:59 pm
by Ayr3s
Okay, so I think this one is easier on the CPU than the last one. I removed all the forking and left only malloc and memset. It still has the infinite loop, but now it waits a little when it fails to allocate 256 bytes of RAM. The kernel kills it very quickly after starting.
Thanks guys!

Code: Select all
#include <unistd.h>

int main() {
void *block = NULL;
int count = 0;
int blocksize = 1024*1024;
while(1){
  block = malloc(blocksize);
    if (!block){
        blocksize = blocksize/2;
    }
    else {
        memset(block, 1, blocksize);
    }
    if (blocksize < 256){
        blocksize = 1024*1024;
        sleep(30);
    }
} //while loop
return 0;
} //main


Re: Memory hog experiment

PostPosted: Fri Jun 19, 2015 7:36 am
by pretentious
I like stuff like this. My version was just mallocing a billion times in a loop until the system froze, I've never learned about forking.

It's subtle, but I think you've reached a mile stone in your jurney when it occurs to you, how to overwhelm a system with 3 lines of code, without any special privileges.