author | dcubed |
Thu, 22 Dec 2011 18:35:48 -0800 | |
changeset 11359 | 9e53808b2128 |
permissions | -rw-r--r-- |
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.util.*; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
26 |
|
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
27 |
public class RetransformBigClassAgent { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
28 |
private static int N_RETRANSFORMS = 1000; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
29 |
public static Class clz; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
30 |
public static volatile boolean doneRetransforming = false; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
31 |
|
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
32 |
// just retransform the class in a loop via a Timer |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
33 |
public static void premain(String agentArgs, final Instrumentation inst) throws Exception { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
34 |
String s = agentArgs.substring(0, agentArgs.indexOf(".class")); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
35 |
clz = Class.forName(s.replace('/', '.')); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
36 |
|
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
37 |
ClassFileTransformer trans = new SimpleIdentityTransformer(); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
38 |
inst.addTransformer(trans, true /* canRetransform */); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
39 |
|
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
40 |
System.gc(); // throw away anything we can before we start testing |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
41 |
|
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
42 |
new Timer(true).schedule(new TimerTask() { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
43 |
public void run() { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
44 |
try { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
45 |
int i; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
46 |
System.out.println("Retransforming"); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
47 |
for (i = 0; i < N_RETRANSFORMS; i++) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
48 |
inst.retransformClasses(clz); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
49 |
System.gc(); // throw away anything we can |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
50 |
} |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
51 |
System.out.println("Retransformed " + i + " times."); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
52 |
RetransformBigClassAgent.doneRetransforming = true; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
53 |
} |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
54 |
catch (Exception e) { e.printStackTrace(); } |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
55 |
} |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
56 |
}, 500); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
57 |
} |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
diff
changeset
|
58 |
} |