0

I came up with the following method. I am getting a stackoverflow error. Is there a faster way?

static void looper(){
        long a = 0;
        a++;
        System.out.println(a);
        looper();
    } 
JasSy
  • 583
  • 5
  • 17
  • 1
    `static void loper(){loper();}`? Why? – Bathsheba Feb 09 '16 at 11:28
  • 1
    When you want to get a `StackOverflowError`, then just call `looper` as the first statement. No need to create, increatement and print `a`. – Tom Feb 09 '16 at 11:29
  • 2
    If you just want to run out of memory, why not just allocate a _massive_ array? – ajshort Feb 09 '16 at 11:31
  • @Tom thanks. Makes sense. – JasSy Feb 09 '16 at 11:35
  • @ajshort Massive array? – JasSy Feb 09 '16 at 11:36
  • @Bathsheba Well was an interview question and this is my answer. Seems I got it wrong. – JasSy Feb 09 '16 at 11:36
  • StackOverflowError is not the same as OutOfMemoryError. You should say fastest way to run out of stack frames / memory :P – TheLostMind Feb 09 '16 at 11:40
  • @TheLostMind Hm seems I totally got it wrong then. What would be fastest way to hit OutOfMemoryError then. I am trying to find fastest way to run out of memory. – JasSy Feb 09 '16 at 11:42
  • @JasSy - Well, if the interviewer meant *any memory*, then the answers posted above (in comments) are right. Else just keep creating objects and keep adding to them to a List in a loop to get OutOfMemory (heap-space) – TheLostMind Feb 09 '16 at 11:44
  • @TheLostMind So guess something along the lines of List ls = new List(); while(true){ls.add(1);} Thanks. – JasSy Feb 09 '16 at 11:50
  • @JasSy - Well.. I would prefer `ls.add(new Integer("1"))` to make things faster. Integer class keeps a local cache of values between -128 to 127. Why not just create new `Runnable` instances and add them to the List? – TheLostMind Feb 09 '16 at 11:52

1 Answers1

1

A fast way to get an OutOfMemoryError is:

final int[] memoryGrave = new int[Integer.MAX_VALUE];

Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

(You can find more about max array sizes in this question: Do Java arrays have a maximum size?)

But this might not be what you're looking for, since it is possible that the JVM checks the requested array size and throws that error before actually running out of memory.
But you can alter that code a bit, to do that "correctly":

final List<int[]> memoryGrave = new ArrayList<>();
while(true) {
    memoryGrave.add(new int[Integer.MAX_VALUE - 5]);
}

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

But if this is "fastest way" to get that error (and really run out of memory) can still be discussed.


When you wrap the above code into a try/finally, then you can measure the time it needs to throw that error:

long start = System.currentTimeMillis();
try {
    final List<int[]> memoryGrave = new ArrayList<>();
    while (true) {
        memoryGrave.add(new int[Integer.MAX_VALUE - 5]);
    }
} finally {
    System.out.println(System.currentTimeMillis() - start);
}

On my machine it needs round about 30 ms.

Community
  • 1
  • 1
Tom
  • 16,842
  • 17
  • 45
  • 54