SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
Class Loader
By: Prateek Jain
Fundamentals
1. Class loader delegation
a. The class loader delegation model is the graph of class loaders that pass
loading requests to each other.
b. The bootstrap class loader is the root of this graph.
c. Class loaders are created with a single delegation parent and looks for class
at:

i. Cache.
ii. Parent.
iii. Self.
Fundamentals
2. The parent class loader is always given the opportunity to load a class first.
3. Due to point #2, a classloader can only see classes loaded by itself or its
parent/ancestor classloaders and not by children.
4. The bootstrap class loader cannot be instantiated by java code.
Fundamentals
5. The extension (standard extensions) class loader is responsible to load
classes from the extensions directory (jre/lib/ext).
6. The system (application)class loader is responsible for loading code from
the path specified by the CLASSPATH environment variable. This can be
returned by:
ClassLoader.getySystemClassLoader();
Phases of class loading
1. Loading
2. Linking
3. Initializing
Phases explained
1. Loading phase
a. Consists of locating the required class file and loading in the
bytecode.
b. It gives a very basic memory structure to the class object.
c. Methods, fields and other referenced classes are not dealt with at this
stage.
Phases explained
1. Linking phase
a. Bytecode verification, the class loader performs checks on the bytecodes of
the class to ensure it is well formed and well behaved.
b. Class preparation, prepares the necessary data structures within each class
like fields, methods and implemented interfaces.
c. Resolving, the class loader loads all the other classes referenced by
particular class.
Phases explained
1. Initializing phase
a. Any static initializers contained within a class are executed.
Note: At the end of this phase, all static fields are given their default values.
Loading Types
Explicit loading
1. via, cl.loadClass() [cl is an instance of ClassLoader].
2. Class.forName().

When one of these methods is invoked, the class whose name is specified as an argument is loaded
by the class loader. If the class is already loaded, then a reference is simply returned; otherwise,
the loader goes through the delegation model to load the class.
Loading Types
Implicit loading
Occurs when a class is loaded as result of a reference, instantiation, or
inheritance (not via an explicit method call). In each of these cases, the
loading is initiated under the covers and the JVM resolves the necessary
references and loads the class.
Problems with class
loaders
1. Example1
2. Visibility of classes.
Problems with class
loaders
3. When over riding loadClass().
If class loaders only use the standard delegation model, then there is no need to override the
loadClass() method. However, if a different model is required, then loadClass() must be
overridden, in which case there are special considerations that must be taken into account.
public Class loadClass(String name) throws ClassNotFoundException
{
return findClass(name);
}

Although this looks reasonable, a call to this method results in the
following exception:
Exception in thread "main" java.lang.NoClassDefFoundError:
java/lang/Object
Because the overridden loadClass() method never delegates to its
parent.
Problems with class
loaders
improved implementation
public Class loadClass(String name) throws ClassNotFoundException {
Class c = null;
try {
c = getParent().loadClass(name);
} catch (ClassNotFoundException e) {
}
if(c == null)
c = findClass(name);
return c;
}
Problems with class
loaders
Problem with serialization and GC
The collector examines the class loader data structures to determine which
classes are live -- that is, are not garbage collectable.
When the class loader is dereferenced, the classes that it loaded are not garbage collectable. This is
because there is a live reference to the serialized class from the ObjectStreamClass lookup table.
ObjectStreamClass is a primordial class and therefore is never garbage collected. The lookup
table is referenced from a static field in ObjectStreamClass and is kept in the class itself rather
than in an instance of it. As a result, the reference to serialized class exists for the lifetime of the
JVM, and the class thus cannot be garbage collected. Importantly, the serialized class has a
reference to its defining class loader, and so it cannot be completely dereferenced either.
Problems with class
loaders
Class loader deadlocks
A class loader deadlock occurs when two threads each own a lock on two different class loaders, and
both threads are waiting for the lock owned by the other thread. Both threads will wait indefinitely
for the lock on the other class loader, and so they become deadlocked.
Problems with class
loaders
Deadlock Scenario

Class Hierarchy:
class A extends B
class C extends D
ClassLoader Delegation Hierarchy:
Custom Classloader CL1:
directly loads class A
delegates to custom ClassLoader CL2 for class B
Custom Classloader CL2:
directly loads class C
delegates to custom ClassLoader CL1 for class D
Thread 1:
Use CL1 to load class A (locks CL1)
defineClass A triggers
loadClass B (try to lock CL2)
Thread 2:
Use CL2 to load class C (locks CL2)
defineClass C triggers
loadClass D (try to lock CL1)
Solution to deadlock
The Java SE 7 release includes the concept of a parallel capable class loader.
Loading a class by a parallel capable class loader now synchronizes on the
pair consisting of the class loader and the class name.
Thread 1:
Use CL1 to load class A (locks CL1+A)
defineClass A triggers
loadClass B (locks CL2+B)
Thread 2:
Use CL2 to load class C (locks CL2+C)
defineClass C triggers
loadClass D (locks CL1+D)

for more details: http://docs.oracle.com/javase/7/docs/technotes/guides/lang/cl-mt.html
Q&A

Contenu connexe

Tendances

Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)
Abhishek Khune
 

Tendances (20)

Understanding Java Dynamic Proxies
Understanding Java Dynamic ProxiesUnderstanding Java Dynamic Proxies
Understanding Java Dynamic Proxies
 
Class loaders
Class loadersClass loaders
Class loaders
 
Dynamic Proxy by Java
Dynamic Proxy by JavaDynamic Proxy by Java
Dynamic Proxy by Java
 
55 new things in Java 7 - Devoxx France
55 new things in Java 7 - Devoxx France55 new things in Java 7 - Devoxx France
55 new things in Java 7 - Devoxx France
 
Java basics
Java basicsJava basics
Java basics
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Java Tutorial 1
Java Tutorial 1Java Tutorial 1
Java Tutorial 1
 
Basics of java
Basics of javaBasics of java
Basics of java
 
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIsCS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)
 
Basics of java programming language
Basics of java programming languageBasics of java programming language
Basics of java programming language
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
2. hello java
2. hello java2. hello java
2. hello java
 
Java Intro
Java IntroJava Intro
Java Intro
 
Class method object
Class method objectClass method object
Class method object
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Inheritance and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In Java
 
Java basic introduction
Java basic introductionJava basic introduction
Java basic introduction
 
Java bcs 21_vision academy_final
Java bcs 21_vision academy_finalJava bcs 21_vision academy_final
Java bcs 21_vision academy_final
 
Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz
 

Similaire à Java Classloaders

Class loader basic
Class loader basicClass loader basic
Class loader basic
명철 강
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
venud11
 
Perils Of Url Class Loader
Perils Of Url Class LoaderPerils Of Url Class Loader
Perils Of Url Class Loader
Kaniska Mandal
 

Similaire à Java Classloaders (20)

Diving into Java Class Loader
Diving into Java Class LoaderDiving into Java Class Loader
Diving into Java Class Loader
 
Class
ClassClass
Class
 
Class
ClassClass
Class
 
Class
ClassClass
Class
 
Class loader basic
Class loader basicClass loader basic
Class loader basic
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questions
 
Static binding
Static bindingStatic binding
Static binding
 
Java14
Java14Java14
Java14
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
 
Perils Of Url Class Loader
Perils Of Url Class LoaderPerils Of Url Class Loader
Perils Of Url Class Loader
 
Java assignment help
Java assignment helpJava assignment help
Java assignment help
 
Java tips
Java tipsJava tips
Java tips
 
LISP: Object Sytstem Lisp
LISP: Object Sytstem LispLISP: Object Sytstem Lisp
LISP: Object Sytstem Lisp
 
LISP:Object System Lisp
LISP:Object System LispLISP:Object System Lisp
LISP:Object System Lisp
 
JVM
JVMJVM
JVM
 
Java Reflection Concept and Working
Java Reflection Concept and WorkingJava Reflection Concept and Working
Java Reflection Concept and Working
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Dernier (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Java Classloaders

  • 2. Fundamentals 1. Class loader delegation a. The class loader delegation model is the graph of class loaders that pass loading requests to each other. b. The bootstrap class loader is the root of this graph. c. Class loaders are created with a single delegation parent and looks for class at: i. Cache. ii. Parent. iii. Self.
  • 3. Fundamentals 2. The parent class loader is always given the opportunity to load a class first. 3. Due to point #2, a classloader can only see classes loaded by itself or its parent/ancestor classloaders and not by children. 4. The bootstrap class loader cannot be instantiated by java code.
  • 4.
  • 5. Fundamentals 5. The extension (standard extensions) class loader is responsible to load classes from the extensions directory (jre/lib/ext). 6. The system (application)class loader is responsible for loading code from the path specified by the CLASSPATH environment variable. This can be returned by: ClassLoader.getySystemClassLoader();
  • 6. Phases of class loading 1. Loading 2. Linking 3. Initializing
  • 7.
  • 8. Phases explained 1. Loading phase a. Consists of locating the required class file and loading in the bytecode. b. It gives a very basic memory structure to the class object. c. Methods, fields and other referenced classes are not dealt with at this stage.
  • 9. Phases explained 1. Linking phase a. Bytecode verification, the class loader performs checks on the bytecodes of the class to ensure it is well formed and well behaved. b. Class preparation, prepares the necessary data structures within each class like fields, methods and implemented interfaces. c. Resolving, the class loader loads all the other classes referenced by particular class.
  • 10. Phases explained 1. Initializing phase a. Any static initializers contained within a class are executed. Note: At the end of this phase, all static fields are given their default values.
  • 11. Loading Types Explicit loading 1. via, cl.loadClass() [cl is an instance of ClassLoader]. 2. Class.forName(). When one of these methods is invoked, the class whose name is specified as an argument is loaded by the class loader. If the class is already loaded, then a reference is simply returned; otherwise, the loader goes through the delegation model to load the class.
  • 12. Loading Types Implicit loading Occurs when a class is loaded as result of a reference, instantiation, or inheritance (not via an explicit method call). In each of these cases, the loading is initiated under the covers and the JVM resolves the necessary references and loads the class.
  • 13. Problems with class loaders 1. Example1 2. Visibility of classes.
  • 14. Problems with class loaders 3. When over riding loadClass(). If class loaders only use the standard delegation model, then there is no need to override the loadClass() method. However, if a different model is required, then loadClass() must be overridden, in which case there are special considerations that must be taken into account. public Class loadClass(String name) throws ClassNotFoundException { return findClass(name); } Although this looks reasonable, a call to this method results in the following exception: Exception in thread "main" java.lang.NoClassDefFoundError: java/lang/Object Because the overridden loadClass() method never delegates to its parent.
  • 15. Problems with class loaders improved implementation public Class loadClass(String name) throws ClassNotFoundException { Class c = null; try { c = getParent().loadClass(name); } catch (ClassNotFoundException e) { } if(c == null) c = findClass(name); return c; }
  • 16. Problems with class loaders Problem with serialization and GC The collector examines the class loader data structures to determine which classes are live -- that is, are not garbage collectable. When the class loader is dereferenced, the classes that it loaded are not garbage collectable. This is because there is a live reference to the serialized class from the ObjectStreamClass lookup table. ObjectStreamClass is a primordial class and therefore is never garbage collected. The lookup table is referenced from a static field in ObjectStreamClass and is kept in the class itself rather than in an instance of it. As a result, the reference to serialized class exists for the lifetime of the JVM, and the class thus cannot be garbage collected. Importantly, the serialized class has a reference to its defining class loader, and so it cannot be completely dereferenced either.
  • 17. Problems with class loaders Class loader deadlocks A class loader deadlock occurs when two threads each own a lock on two different class loaders, and both threads are waiting for the lock owned by the other thread. Both threads will wait indefinitely for the lock on the other class loader, and so they become deadlocked.
  • 18. Problems with class loaders Deadlock Scenario Class Hierarchy: class A extends B class C extends D ClassLoader Delegation Hierarchy: Custom Classloader CL1: directly loads class A delegates to custom ClassLoader CL2 for class B Custom Classloader CL2: directly loads class C delegates to custom ClassLoader CL1 for class D Thread 1: Use CL1 to load class A (locks CL1) defineClass A triggers loadClass B (try to lock CL2) Thread 2: Use CL2 to load class C (locks CL2) defineClass C triggers loadClass D (try to lock CL1)
  • 19. Solution to deadlock The Java SE 7 release includes the concept of a parallel capable class loader. Loading a class by a parallel capable class loader now synchronizes on the pair consisting of the class loader and the class name. Thread 1: Use CL1 to load class A (locks CL1+A) defineClass A triggers loadClass B (locks CL2+B) Thread 2: Use CL2 to load class C (locks CL2+C) defineClass C triggers loadClass D (locks CL1+D) for more details: http://docs.oracle.com/javase/7/docs/technotes/guides/lang/cl-mt.html
  • 20. Q&A