OOME.java revision 1870:4aa2e64eff30
1130803Smarcel/*
2130803Smarcel * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3130803Smarcel * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4130803Smarcel *
5130803Smarcel * This code is free software; you can redistribute it and/or modify it
6130803Smarcel * under the terms of the GNU General Public License version 2 only, as
7130803Smarcel * published by the Free Software Foundation.
8130803Smarcel *
9130803Smarcel * This code is distributed in the hope that it will be useful, but WITHOUT
10130803Smarcel * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11130803Smarcel * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12130803Smarcel * version 2 for more details (a copy is included in the LICENSE file that
13130803Smarcel * accompanied this code).
14130803Smarcel *
15130803Smarcel * You should have received a copy of the GNU General Public License version
16130803Smarcel * 2 along with this work; if not, write to the Free Software Foundation,
17130803Smarcel * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18130803Smarcel *
19130803Smarcel * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20130803Smarcel * or visit www.oracle.com if you need additional information or have any
21130803Smarcel * questions.
22130803Smarcel */
23130803Smarcel
24130803Smarcel
25130803Smarcelimport java.util.LinkedList;
26130803Smarcel
27130803Smarcel/*
28130803Smarcel * @test
29130803Smarcel * @summary Slowly eat all memory in an infinite loop
30130803Smarcel * @run main/othervm Crash
31130803Smarcel */
32130803Smarcelpublic class OOME {
33130803Smarcel    @SuppressWarnings ("UnusedDeclaration")
34130803Smarcel    private static Object garbage;
35130803Smarcel    public static void main(String args[]) {
36130803Smarcel
37130803Smarcel        int chunkSize = 0x8000;
38130803Smarcel        LinkedList<int[]> list = new LinkedList<>();
39130803Smarcel        garbage = list;
40130803Smarcel
41130803Smarcel        while (true) {
42130803Smarcel            try {
43130803Smarcel                list.add(new int[chunkSize]);
44130803Smarcel            } catch (OutOfMemoryError e) {
45130803Smarcel                chunkSize >>= 1;
46130803Smarcel            }
47130803Smarcel        }
48130803Smarcel    }
49130803Smarcel}
50130803Smarcel