File: ClassUnloader.java

package info (click to toggle)
openjdk-11 11.0.16%2B8-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 784,576 kB
  • sloc: java: 5,128,021; xml: 1,192,224; cpp: 1,124,021; ansic: 422,433; javascript: 155,577; sh: 17,084; objc: 13,327; python: 4,522; asm: 3,570; makefile: 2,858; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (352 lines) | stat: -rw-r--r-- 12,149 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 *  Warning! Using this component need VM option -XX:-UseGCOverheadLimit
 *
 */

package nsk.share;

import java.util.*;
import nsk.share.gc.gp.*;
import nsk.share.test.ExecutionController;
import nsk.share.test.Stresser;

/**
 * The <code>ClassUnloader</code> class allows to force VM to unload class(es)
 * using memory stressing technique.
 *
 * <p>The method <code>unloadClass()</code> is provided which eats memory
 * to enforce GC to cleanup the heap. So, if all references to a class
 * and its loader are canceled, this may result in unloading the class.
 *
 * <p>ClassUnloader mainly intends to unload a class which was loaded
 * with especial <code>ClassUnloader.loadClass()</code> method.
 * A class is considered unloaded if its class loader is finalized.
 * If there no finalization of class loader detected for some timeout,
 * class is considered still loaded and method returns <i>false</i>.
 *
 * <p>Such finalization control applies only to a class loaded by
 * ClassUnloader's <code>loadClass()</code> method. Otherwise, if there
 * was no such class loaded, <code>unloadClass()</code> doesn't wait
 * for a timeout and always returns <i>false</i>.
 *
 * <p>By default internal class loader of <code>CustomClassLoader</code> class
 * is used for loading classes. This class loader can load class from .class file
 * located in the specified directory.
 * Application may define its own class loader, which may load classes using
 * any other technique. Such class loader should be derived from base
 * <code>CustomClassLoader</code> class, and set by <code>setClassLoader()</code>
 * method.
 *
 * @see #setClassLoader(CustomClassLoader)
 * @see #loadClass(String)
 * @see #loadClass(String, String)
 * @see #unloadClass()
 */
public class ClassUnloader {

    /**
     * Class name of default class loader.
     */
    public static final String INTERNAL_CLASS_LOADER_NAME = "nsk.share.CustomClassLoader";

    /**
     * Whole amount of time in milliseconds to wait for class loader finalization.
     */
    private static final int WAIT_TIMEOUT = 15000;

    /**
     * Piece of time in milliseconds to wait in a loop for class loader finalization.
     */
    private static final int WAIT_DELTA = 1000;

    /**
     * Has class loader been finalized or not.
     */
    volatile boolean finalized = false;

    /**
     * Current class loader used for loading classes.
     */
    private CustomClassLoader customClassLoader = null;

    /**
     * List of classes loaded with current class loader.
     */
    private Vector<Class<?>> classObjects = new Vector<Class<?>>();

    /**
     * Class object of the first class been loaded with current class loader.
     * To get the rest loaded classes use <code>getLoadedClass(int)</code>.
     * The call <code>getLoadedClass()</code> is effectively equivalent to the call
     * <code>getLoadedClass(0)</code>
     *
     * @return class object of the first loaded class.
     *
     * @see #getLoadedClass(int)
     */
    public Class<?> getLoadedClass() {
        return classObjects.get(0);
    }

    /**
     * Returns class objects at the specified index in the list of classes loaded
     * with current class loader.
     *
     * @return class objects at the specified index.
     */
    public Class<?> getLoadedClass(int index) {
        return classObjects.get(index);
    }

    /**
     * Creates new instance of <code>CustomClassLoader</code> class as the current
     * class loader and clears the list of loaded classes.
     *
     * @return created instance of <code>CustomClassLoader</code> class.
     *
     * @see #getClassLoader()
     * @see #setClassLoader(CustomClassLoader)
     */
    public CustomClassLoader createClassLoader() {
        customClassLoader = new CustomClassLoader(this);
        classObjects.removeAllElements();

        return customClassLoader;
    }

    /**
     * Sets new current class loader and clears the list of loaded classes.
     *
     * @see #getClassLoader()
     * @see #createClassLoader()
     */
    public void setClassLoader(CustomClassLoader customClassLoader) {
        this.customClassLoader = customClassLoader;
        classObjects.removeAllElements();
        customClassLoader.setClassUnloader(this);
    }

    /**
     * Returns current class loader or <i>null</i> if not yet created or set.
     *
     * @return class loader object or null.
     *
     * @see #createClassLoader()
     * @see #setClassLoader(CustomClassLoader)
     */
    public CustomClassLoader getClassLoader() {
        return customClassLoader;
    }

    /**
     * Loads class for specified class name using current class loader.
     *
     * <p>Current class loader should be set and capable to load class using only
     * given class name. No other information such a location of .class files
     * is passed to class loader.
     *
     * @param className name of class to load
     *
     * @throws ClassNotFoundException if no bytecode found for specified class name
     * @throws Failure if current class loader is not specified;
     *                 or if class was actually loaded with different class loader
     *
     * @see #loadClass(String, String)
     */
    public void loadClass(String className) throws ClassNotFoundException {

        if (customClassLoader == null) {
            throw new Failure("No current class loader defined");
        }

        Class<?> cls = Class.forName(className, true, customClassLoader);

        // ensure that class was loaded by current class loader
        if (cls.getClassLoader() != customClassLoader) {
            throw new Failure("Class was loaded by unexpected class loader: " + cls.getClassLoader());
        }

        classObjects.add(cls);
    }

    /**
     * Loads class from .class file located into specified directory using
     * current class loader.
     *
     * <p>If there is no current class loader, then default class loader
     * is created using <code>createClassLoader()</code>. Parameter <i>classDir</i>
     * is passed to class loader using <code>CustomClassLoader.setClassPath()</code>
     * method before loading class.
     *
     * @param className name of class to load
     * @param classDir path to .class file location
     *
     * @throws ClassNotFoundException if no .class file found
     *          for specified class name
     * @throws Failure if class was actually loaded with different class loader
     *
     * @see #loadClass(String)
     * @see CustomClassLoader#setClassPath(String)
     */
    public void loadClass(String className, String classDir) throws ClassNotFoundException {

        if (customClassLoader == null) {
            createClassLoader();
        }

        customClassLoader.setClassPath(classDir);
        loadClass(className);
    }

    /**
     * Forces GC to unload previously loaded classes by cleaning all references
     * to class loader with its loaded classes and eating memory.
     *
     * @return  <i>true</i> if classes unloading has been detected
             or <i>false</i> otherwise
     *
     * @throws  Failure if exception other than OutOfMemoryError
     *           is thrown while eating memory
     *
     * @see #eatMemory()
     */
    public boolean unloadClass(ExecutionController stresser) {

        finalized = false;

        // free references to class and class loader to be able for collecting by GC
        long waitTimeout = (customClassLoader == null) ? 0 : WAIT_TIMEOUT;
        classObjects.removeAllElements();
        customClassLoader = null;

        // force class unloading by eating memory pool
        eatMemory(stresser);

        // give GC chance to run and wait for finalization
        long timeToFinish = System.currentTimeMillis() + waitTimeout;
        while (!finalized && System.currentTimeMillis() < timeToFinish) {
            if (!stresser.continueExecution()) {
                return false;
            }
            try {
                // suspend thread for a while
                Thread.sleep(WAIT_DELTA);
            } catch (InterruptedException e) {
                throw new Failure("Unexpected InterruptedException while class unloading: " + e);
            }
        }

        // force GC to unload marked class loader and its classes
        if (finalized) {
            Runtime.getRuntime().gc();
            return true;
        }

        // class loader has not been finalized
        return false;
    }

    public boolean unloadClass() {
        Stresser stresser = new Stresser() {

            @Override
            public boolean continueExecution() {
                return true;
            }

        };
        return unloadClass(stresser);
    }

    /**
     * Stresses memory by allocating arrays of bytes.
     *
     * Note that this method can throw Failure if any exception
     * is thrown while eating memory. To avoid OOM while allocating
     * exception we preallocate it before the lunch starts. It means
     * that exception stack trace does not correspond to the place
     * where exception is thrown, but points at start of the method.
     *
     * @throws  Failure if exception other than OutOfMemoryError
     *           is thrown while eating memory
     */
    public static void eatMemory(ExecutionController stresser) {
        GarbageUtils.eatMemory(stresser, 50, 1024, 2);

        /*
         * System.runFinalization() may potentially fail with OOM. This is why
         * System.runFinalization() is repeated several times.
         */
        for (int i = 0; i < 10; ++i) {
            try {
                if(!stresser.continueExecution()) {
                    return;
                }
                System.runFinalization();
                break;
            } catch (OutOfMemoryError e) {
            }
        }
    }

        /**
     * Stresses memory by allocating arrays of bytes.
     *
     * Note that this method can throw Failure if any exception
     * is thrown while eating memory. To avoid OOM while allocating
     * exception we preallocate it before the lunch starts. It means
     * that exception stack trace does not correspond to the place
     * where exception is thrown, but points at start of the method.
     *
     * @throws  Failure if exception other than OutOfMemoryError
     *           is thrown while eating memory
     */
    public static void eatMemory() {
        Stresser stresser = new Stresser() {

            @Override
            public boolean continueExecution() {
                return true;
            }

        };
        GarbageUtils.eatMemory(stresser, 50, 1024, 2);
        /*
         * System.runFinalization() may potentially fail with OOM. This is why
         * System.runFinalization() is repeated several times.
         */
        for (int i = 0; i < 10; ++i) {
            try {
                if(!stresser.continueExecution()) {
                    return;
                }
                System.runFinalization();
                break;
            } catch (OutOfMemoryError e) {
            }
        }
    }
}