Understanding ClassLoaders
-
Upload
martin-skurla -
Category
Technology
-
view
236 -
download
0
Transcript of Understanding ClassLoaders
![Page 1: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/1.jpg)
Understanding classloaders
![Page 2: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/2.jpg)
What will you learn
● How classloaders work● reasons, Class and Classloader basics, semantics, Java SE/EE
delegation mechanism, ClassLoader API
● Modern classloaders● Implementation of simple classloader● Pitfalls
![Page 3: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/3.jpg)
Reasons for ClassLoaders
● when you need to load classes from different places that just classpath
● applets● plugin systems
● when you need support for running the same library with multiple versions within the same JVM
● module systems
● necessary for implementing "hotswap" functionality● web containers & application servers
● JNI native libraries
![Page 4: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/4.jpg)
java.lang.Class overview
● represents a type (class, interface, enum, annotation, primitive marker) in the JVM
● allocates memory in both heap and permanent generation
● is always referenced by every object instance of the class
● holds a reference to its ClassLoader● is able to load resources
![Page 5: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/5.jpg)
java.lang.Class
● can be acquired● by class literal String.class● from object object.getClass();● through ClassLoader Class.forName(String name);
![Page 6: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/6.jpg)
Class identity
● "every Class is uniquely identified by its fully qualified name"
● starting from Java 1.2 not true any more● because of security reasons
● JLS 4.3.4 (When reference types are the same)● both are class or interface types● both are defined by the same classloader● both have the same binary name
![Page 7: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/7.jpg)
Class identity
● changes added higher complexity resulting in interesting new classloading options as well as new problems and pitfalls
![Page 8: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/8.jpg)
ClassLoader overview
● instance of java.lang.ClassLoader● responsible for loading classes● knows how to transform raw data (bytes) into
instance of Class● holds a strong reference to all classes it loads● has an optional parent for delegating
![Page 9: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/9.jpg)
ClassLoader references
● starting from Java 1.2, 4 types of references on JVM:● strong reference● soft reference● weak reference● phantom reference
![Page 10: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/10.jpg)
ClassLoader lazy loading principle
public class Foo { public static void main(String[] args) { pack.Bar bar = new pack.Bar(); bar.coolMethod(); }}
![Page 11: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/11.jpg)
ClassLoader lazy loading principle
public class Foo { public static void main(String[] args) { pack.Bar bar = new pack.Bar(); bar.coolMethod(); }}
![Page 12: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/12.jpg)
ClassLoader lazy loading principle
public class Foo { public static void main(String[] args) { pack.Bar bar = new pack.Bar(); bar.coolMethod(); }}
semantically causes similar call to
Foo.class.getClassLoader().loadClass("pack.Bar");
![Page 13: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/13.jpg)
ClassLoader delegation mechanism
● ClassLoader has a parent ClassLoader● parent ClassLoader should be consulted first
● "parent first delegation model"● the reason is to avoid loading the same class several times● every ClassLoader except OSGi and Web App ClassLoader
● exact delegation depends on ClassLoader implementation
![Page 14: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/14.jpg)
Java SE delegation mechanism
![Page 15: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/15.jpg)
Java SE delegation mechanism
![Page 16: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/16.jpg)
Java SE delegation mechanism
![Page 17: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/17.jpg)
Java SE delegation mechanism
![Page 18: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/18.jpg)
Java EE delegation mechanism
● no Java EE specification provides a standard for class loading
● however, Java EE defines the visibility and sharing of classes between different modules
● Web Application ClassLoader is different from all others
● instead of first asking its parent, it will first look itself for the class and then ask the parent
● each WAR of an EAR gets its own ClassLoader● this allows separate namespaces for applications in the same
container
![Page 19: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/19.jpg)
Java EE delegation mechanism
![Page 20: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/20.jpg)
Java EE delegation mechanism
![Page 21: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/21.jpg)
Java EE delegation mechanism
![Page 22: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/22.jpg)
Java EE delegation mechanism
![Page 23: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/23.jpg)
Java EE delegation mechanism
![Page 24: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/24.jpg)
Java EE delegation mechanism
![Page 25: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/25.jpg)
ClassLoader API
● public Class<?> loadClass(String name);
● protected Class<?> findClass(String name);
● protected final Class<?>
defineClass(String name, byte[] b, inf off, int len);
● public ClassLoader getParent();
● public URL getResource(String name);
● public InputStream getResourceAsStream(String name);
● public Enumeration<URL> getResources();
![Page 26: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/26.jpg)
Showcase
● OpenJDK java.lang.ClassLoader source code examination
● flow of 3 dependent ClassLoaders
● simple ClassLoader implementation● ClassNotFoundException vs NoClassDefFoundError
![Page 27: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/27.jpg)
Modern ClassLoaders
● because tree hierarchy is not enough● in terms of isolation, performance, restrictions
● modern way● each JAR has its own ClassLoader● ClassLoaders are siblings, with one central repository● each JAR explicitly decalres packages it exports and imports● repository can find relevant ClassLoaders by package● libraries packed into separate modules
● examples● OSGi, NetBeans module system, JBoss Modules
![Page 28: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/28.jpg)
Showcase
● Multiparent ClassLoaders● solving multiple versions of the same library problem
![Page 29: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/29.jpg)
Common problems
● too many various errors can be thrown● AbstractMethodError, IllegalAccessError,
IncompatibleClassChangeError, LinkageError, NoClassDefFoundError, NoSuchFieldError, NoSuchMethodError, OutOfMemoryError
● and exceptions too● ClassCastException, ClassNotFoundException
● possible deadlocks● most of them are caused by invalid synchronization of
classes between IDE and deployed packages
![Page 30: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/30.jpg)
Common problems
● many of them can be tracked by using tools● URLClassLoader.getURLs();● ClassLoader.getResource(String);● javap tool● java -verbose:class
![Page 31: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/31.jpg)
Common pitfalls + code samples
● ClassCastException● because of Java 1.2 changes in ClassLoader architecture
● OutOfMemoryError (both Heap and Permgen)● often during HotDeploy● because ClassLoaders are leaking● because there are too many different classes
● Singletons (and not only them)● because of static attributes
![Page 32: Understanding ClassLoaders](https://reader031.fdocuments.net/reader031/viewer/2022030315/5882bdcc1a28abb2478b5471/html5/thumbnails/32.jpg)
Interesting projects
● JRebel● Javaleon● HotSwap● Tattletale● OSGi● NetBeans module system● JBoss Modules● Project jigsaw