Class Loading Flashcards
Binary class format
Format:
- init header `cafebabe’ to identify actual classes vs other binaries
- major/minor version numbers
- count of entries in constant pool
- actual constant pool data
- Superclass/interfaces - these reference constant pool entries for the class itself
- Information about fields/methods (themselves complex structures)
- Executable code for methods; code attributes contained within method definitions
Constant pool data
Stores all constants used by the class definition
- class and method names, signatures, strings
- Items of variable length: first byte=>type/how to decode
- References to other methods and classes used by this class + declarations for this class and its method
Attributes
Some container for data
e. g. method bytecode, constants, value of a constant field
- JVM ignores attributes of unknown type - flexibility for extending use of attributes to serve other purposes [metadata for frameworks that work with user classes - C#]
JVM
- stack architecture
- operands: immediate or from constant pool
JVM implementation
- Interpreted: slower than executive native code
- JIT translation: compile before executing the first time: better performance for repeated executions
- Adaptive techniques to monitor program execution, selectively optimise heavily used code
Linking process in C
C => machine code
linking merges code from separately compiled source files + shared library code to form an executable
Linking in Java
- no change to bytecode until loaded into JVM
- linking performed by JVM when class is loaded into memory
- some overhead as classes are initially loaded
- flexibility: e.g.applications can be written to use interfaces with the actual implementations left unspecified until run time [late binding]
Loading classes in Java - when
- only load when needed (but JVM has some flexibility, but must maintain a fixed sequence of class initialisation
- dependencies between classes => recursive loading
- Initial core classes used by even empty Java programs
- Load dependencies only when an instance of the class is about to be created by code / static initialisers
Loading classes in Java - what happens
- Decode binary class format
- Check compatibility with other classes
- Verify sequence of bytecode operations
- Construct java.lang.Class instance to represent new class
- Class instance becomes basis for all instances of the new class created by the JVM
- Class object is the identifier for the loaded class itself – can have multiple copies of the same binary class loaded into the JVM each with its own Class instance - they share the same class name but will be separate classes to the JVM.
Bootstrap class loader
- built into JVM, responsible for loading basic Java class libraries special features: - only load classes found on the boot class path - less validation since these are trusted system classes
Extension class loader
CL for loading classes from standard Java extension APIs
System class loader
for loading classes from the general class path including your application classes
Application defined class loaders - why
Runtime reloading of classes Derived from java.lang.ClassLoader provides core support for building an internal class representation from an array of bytes Each constructed class is owned by the class loader that loaded it ... they keep a map from name => Class instance, for all loaded classes, to be able to find one by name if it's requested again
Class loader tree
1- class loader keeps reference to a parent class loader, defining a tree of class loaders with the bootstrap loader at the root. 2- When an instance of a particular class (identified by name) is needed, whichever class loader initially handles the request normally checks with its parent class loader first before trying to load the class directly. 3- This applies recursively if there are multiple layers of class loaders, so it means that a class will normally be visible not only within the class loader that loaded it, but also to all descendant class loaders. 4- It also means that if a class can be loaded by more than one class loader in a chain, the one furthest up the tree will be the one that actually loads it.
Example: utility of multiple class loaders, J2EE framework
- each J2EE application loaded by the framework needs to have a separate class loader to prevent classes in one application from interfering with other applications
- framework’s code itself will also use one or more other class loaders, to prevent interference to or from applications
- complete set of classloaders make upa