rambrain
memeater.cpp
Go to the documentation of this file.
1 /* rambrain - a dynamical physical memory extender
2  * Copyright (C) 2015 M. Imgrund, A. Arth
3  * mimgrund (at) mpifr-bonn.mpg.de
4  * arth (at) usm.uni-muenchen.de
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <sys/mman.h>
21 #include <iostream>
22 #include <fstream>
23 #include <stdio.h>
24 #include <malloc.h>
25 #include <stdlib.h>
26 
27 using namespace std;
28 
35 int main ( int argc, char **argv )
36 {
37  if ( argc < 2 ) {
38  printf ( "Usage: ./rambrain-memeater <MB to leave free>\n" );
39  return -1;
40  }
41 
42  ifstream meminfo ( "/proc/meminfo", ifstream::in );
43  char line[1024];
44  for ( int c = 0; c < 3; ++c ) {
45  meminfo.getline ( line, 1024 );
46  }
47 
48  printf ( "%s\n", line );
49  long mbytes_avail;
50 
51  char *begin = NULL, *end = NULL;
52  char *pos = line;
53  while ( !end ) {
54  if ( *pos <= '9' && *pos >= '0' ) {
55  if ( begin == NULL ) {
56  begin = pos;
57  }
58  } else {
59  if ( begin != NULL ) {
60  end = pos;
61  }
62  }
63  ++pos;
64  }
65  * ( end + 1 ) = 0x00;
66  mbytes_avail = atol ( begin ) / 1024;
67  long mbytes_holdfree = atoi ( argv[1] );
68  long mb_alloc = mbytes_avail - mbytes_holdfree;
69 
70  printf ( "\nWe detected %lu free mBytes\nAllocating %lu\n", mbytes_avail, mb_alloc );
71  if ( mb_alloc <= 0 ) {
72  printf ( "You don't need to nomnom, we're already fed up." );
73  return -1;
74  }
75 
76 
77  unsigned int chunkSize = 1024 * 1024;
78 
79  void *malloced[mb_alloc];
80  for ( int c = 0; c < mb_alloc; ++c ) {
81  malloced[c] = malloc ( chunkSize );
82  for ( unsigned int d = 0; d < chunkSize; ++d ) {
83  ( ( char * ) malloced[c] ) [d] = 0x07;
84  }
85  }
86  mlockall ( MCL_CURRENT );
87  printf ( "Oink, **burpp**" );
88  int bah ;
89  cin >> bah;
90  munlockall();
91 }
92 
93 
94 
int main(int argc, char **argv)
Provides a binary for a program which eats memory in order to decrease the system memory manually...
Definition: memeater.cpp:35
STL namespace.