jdk/test/java/lang/instrument/RedefineBigClassAgent.java
author dcubed
Thu, 22 Dec 2011 18:35:48 -0800
changeset 11359 9e53808b2128
permissions -rw-r--r--
7121600: Instrumentation.redefineClasses() leaks class bytes Summary: Call JNI ReleaseByteArrayElements() on memory returned by JNI GetByteArrayElements(). Also push test for 7122253. Reviewed-by: acorn, poonam
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11359
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
     1
/*
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
     4
 *
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
     8
 *
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    13
 * accompanied this code).
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    14
 *
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    18
 *
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    21
 * questions.
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    22
 */
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    23
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    24
import java.lang.instrument.*;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    25
import java.net.*;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    26
import java.util.*;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    27
import java.io.*;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    28
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    29
public class RedefineBigClassAgent {
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    30
    // N_REDEFINES = 2000 made my Solaris X86 box crawl on its knees
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    31
    // with the Server VM. The timeout is 600 seconds, but the test
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    32
    // ran for 675 seconds so it took the harness 75 seconds to kill
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    33
    // the test.
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    34
    private static int N_REDEFINES = 1000;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    35
    public static Class clz;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    36
    public static volatile boolean doneRedefining = false;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    37
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    38
    // just read the original class and redefine in a loop via a Timer
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    39
    public static void premain(String agentArgs, final Instrumentation inst) throws Exception {
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    40
        String s = agentArgs.substring(0, agentArgs.indexOf(".class"));
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    41
        clz = Class.forName(s.replace('/', '.'));
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    42
        ClassLoader loader =
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    43
            RedefineBigClassAgent.class.getClassLoader();
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    44
        URL classURL = loader.getResource(agentArgs);
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    45
        if (classURL == null) {
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    46
            throw new Exception("Cannot find class: " + agentArgs);
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    47
        }
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    48
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    49
        int         redefineLength;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    50
        InputStream redefineStream;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    51
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    52
        System.out.println("Reading test class from " + classURL);
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    53
        if (classURL.getProtocol().equals("file")) {
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    54
            File f = new File(classURL.getFile());
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    55
            redefineStream = new FileInputStream(f);
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    56
            redefineLength = (int) f.length();
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    57
        } else {
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    58
            URLConnection conn = classURL.openConnection();
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    59
            redefineStream = conn.getInputStream();
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    60
            redefineLength = conn.getContentLength();
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    61
        }
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    62
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    63
        final byte[] buffer = new byte[redefineLength];
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    64
        new BufferedInputStream(redefineStream).read(buffer);
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    65
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    66
        System.gc();  // throw away anything we can before we start testing
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    67
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    68
        new Timer(true).schedule(new TimerTask() {
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    69
            public void run() {
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    70
                try {
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    71
                    int i;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    72
                    System.out.println("Redefining");
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    73
                    ClassDefinition cld = new ClassDefinition(clz, buffer);
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    74
                    for (i = 0; i < N_REDEFINES; i++) {
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    75
                        inst.redefineClasses(new ClassDefinition[] { cld });
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    76
                        System.gc();  // throw away anything we can
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    77
                    }
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    78
                    System.out.println("Redefined " + i + " times.");
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    79
                    RedefineBigClassAgent.doneRedefining = true;
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    80
                }
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    81
                catch (Exception e) { e.printStackTrace(); }
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    82
            }
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    83
        }, 500);
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    84
    }
9e53808b2128 7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff changeset
    85
}