Reputation: 7886
Is there a utility (for Windows) that uses up memory so I can create a JVM "could not reserve enough space for object heap" error?
I want to use up this memory in a process outside of the JVM.
Upvotes: 2
Views: 2237
Reputation: 23265
Here is a tiny C program for you that will consume the number of bytes specified on the command line:
#include <stdlib.h>
int main(int argc, char *argv[]) {
int bytes = atoi(argv[1]);
char *buf = malloc(bytes);
while (1) {
int i;
for (i = 0; i < bytes; i++) buf[i] += 1;
}
}
Upvotes: -1
Reputation: 1099
You can use up an arbitrary amount of memory by running a number of scripts that look like this:
public static void main(String[] args)
{
List<String> l = new ArrayList<String>();
for (long i = 0 ; i < 100000000l ; i++)
{
l.add(new String("AAAAAAA"));
}
}
With sufficiently large heap space (e.g. -Xmx1024M). The problem with this is that any modern OS will attempt to use virtual memory to allow the application to still function, which will cause your hard drive to thrash rather than run out of memory for the JVM. You may need to set your OS total swap space to something fixed to actually encounter this scenario.
Upvotes: -1
Reputation: 14469
List<Object> leak = new ArrayList<Object>();
while(true) {
leak.add(new Object());
}
Upvotes: -1
Reputation: 8222
Just use the -Xms flag
java -Xms3g org.foo.Main
The above will try to create an initial heap size of 3 GB, just adjust the number so it is larger than the total memory of your system (physical & virtual)
Upvotes: 7
Reputation: 18741
I think you can try with this one:
String s = "b";
for (int i = 0; i < 1000 000; i++) {
s+="b";
}
Because new string will be allocated everytime the line s+="b" is run, it should run out of java heap.
Upvotes: -1