test/jdk/java/util/HashMap/HashMapCloneLeak.java
author jdv
Wed, 15 May 2019 10:10:53 +0530
branchmetal-prototype-branch
changeset 57357 f3beca8f19fc
parent 49483 d374b1634589
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15512
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
     1
/*
49483
d374b1634589 8199777: Deprecate -XX:+AggressiveOpts
thartmann
parents: 47216
diff changeset
     2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
15512
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
     4
 *
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
     7
 * published by the Free Software Foundation.
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
     8
 *
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    13
 * accompanied this code).
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    14
 *
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    18
 *
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    21
 * questions.
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    22
 */
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    23
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    24
/**
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    25
 * @test
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    26
 * @bug 7042126
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    27
 * @summary Verify that we do not leak contents when we clone a HashMap
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    28
 * @author david.buck@oracle.com
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    29
 * @run main/othervm HashMapCloneLeak
49483
d374b1634589 8199777: Deprecate -XX:+AggressiveOpts
thartmann
parents: 47216
diff changeset
    30
 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+EliminateAutoBox -XX:AutoBoxCacheMax=20000 HashMapCloneLeak
15512
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    31
 */
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    32
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    33
import java.util.HashMap;
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    34
import java.lang.ref.WeakReference;
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    35
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    36
public class HashMapCloneLeak {
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    37
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    38
    static WeakReference<Object> wr = null;
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    39
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    40
    // helper method to keep testObject and map out of main method's scope
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    41
    private static HashMap<Integer, Object> makeMap() {
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    42
        HashMap<Integer, Object> map = new HashMap<Integer, Object>();
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    43
        Object testObject = new Object();
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    44
        wr = new WeakReference<Object>(testObject);
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    45
        map.put(42, testObject);
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    46
        return map;
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    47
    }
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    48
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    50
        HashMap<Integer, Object> hm = makeMap();
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    51
        hm = (HashMap<Integer, Object>)hm.clone();
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    52
        hm.clear();
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    53
        // There should no longer be a strong reference to testObject
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    54
        // the WeakReference should be nulled out by GC. If not,
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    55
        // we will hang here until timed out by the test harness.
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    56
        Object[] chain = null;
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    57
        while (wr.get() != null) {
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    58
            try {
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    59
                Object[] allocate = new Object[1000000];
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    60
                allocate[0] = chain;
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    61
                chain = allocate;
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    62
            } catch (OutOfMemoryError oome) {
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    63
                chain = null;
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    64
            }
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    65
            System.gc();
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    66
            Thread.sleep(100);
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    67
        }
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    68
    }
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    69
32046b0f4aa8 7042126: (alt-rt) HashMap.clone implementation should be re-examined
dbuck
parents:
diff changeset
    70
}