Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Garbage Collection What is garbage and how can we deal with it?
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
Dynamic Code Generation in Java. Class Loading Class loading is the process of transforming a byte code (e.g., a.class file) into a Java class A Java.
1 InTroToJCL Introduction to Java Class Loaders. 2 class loader l ia an object responsible for loading classes. The class ClassLoader is an abstract class.
Memory Management Chapter 7.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Chapter 10 Introduction to Arrays
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
CPSC 388 – Compiler Design and Construction
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
Dynamic Code Generation in Java. Class Loading Class loading is the process of transforming a byte code (e.g., a.class file) into a Java class A Java.
JETT 2003 Java.compareTo(C++). JAVA Java Platform consists of 4 parts: –Java Language –Java API –Java class format –Java Virtual Machine.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Memory Management 2010.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
Session-02. Objective In this session you will learn : What is Class Loader ? What is Byte Code Verifier? JIT & JAVA API Features of Java Java Environment.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
CLR: Garbage Collection Inside Out
OOP Languages: Java vs C++
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
The Java Virtual Machine Mike Brunt.  What is the JVM?  Main JVM Suppliers  ColdFusion and the JVM  Java J2EE – Java EE Servlet Containers  Where.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture 10 : Introduction to Java Virtual Machine
© 2004, D. J. Foreman 1 Memory Management. © 2004, D. J. Foreman 2 Building a Module -1  Compiler ■ generates references for function addresses may be.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Runtime System CS 153: Compilers. Runtime System Runtime system: all the stuff that the language implicitly assumes and that is not described in the program.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Constructors & Garbage Collection Ch. 9 – Head First Java.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Processes and Virtual Memory
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Object Oriented Software Development 4. C# data types, objects and references.
Programming Languages and Paradigms Activation Records in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
® July 21, 2004GC Summer School1 Cycles to Recycle: Copy GC Without Stopping the World The Sapphire Collector Richard L. Hudson J. Eliot B. Moss Originally.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Design issues for Object-Oriented Languages
Garbage Collection What is garbage and how can we deal with it?
Partitioned Memory Allocation
Java Programming Language
Memory Management © 2004, D. J. Foreman.
Running An Encrypted Java Program
Java Virtual Machine Complete subject details are available at:
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading  Classloader Family Tree  The Rules of Classloading  Custom Classloading  Breaking the Rules

Java Garbage Collectors 1/2 There are two basic types of Garbage Collector  Stop the world garbage-collectors All threads are suspended Garbage collection is performed All threads are re-started  Non-blocking garbage-collectors Execution is sometimes suspended for a short time before garbage collection is actually performed Java has 4 memory spaces or “heaps”  Objects are allocated, and promoted from one space to the other, the longer they live

Java Garbage Collectors 2/2 There are 3 different “Generations” in Java  Young Generation, divided into 3 spaces Eden Space Surviver From Space Surviver To Space  Tenured Generation For objects that are in current use  Perm Generation For VM objects and byte-code  java.lang.reflect objects  The VM's “Main” thread  The System Classloader

Eden Space 1/4 All Objects and primitives are allocated here Uses a “copy” garbage-collector  Copy is a stop-the-world collector  Two equal sized heaps  Allocate all data into the “active” heap  When the active heap is full, copy the live objects into the inactive heap  The inactive heap becomes the inactive heap Eden space is the smallest heap  This means that Eden space is frequently garbage collected

Eden Space 2/4 Advantages of a copy garbage collector  Very simple form of collection  Simple means it's fast  It has a “stack and compact” effect Data is always allocated on top of the heap When garbage collection occurs: all the live objects are compacted to the bottom of the heap Disadvantages  It's stop-the-world, nothing can execute while collection is happening  Uses 2 heaps, and so double the memory

Eden Space 3/4 (Copy Collection) ActiveActive ActiveActive Allocate Object ActiveActive ActiveActive Active Heap is Full ActiveActive Copy active objects Stop the world ActiveActive Swap Active Heap Restart the world

Eden Space 4/4 (Summary) Because Eden space is small, it is garbage collected often Eden space uses a stop the world garbage collector, but is very fast and so no effect is really visible on application performance Eden space may grow to accommodate larger or more objects  Both heaps will always be kept to the same size After garbage-collection, all remaining objects are moved to “Surviver Space”

Surviver Space 1/2 Contains objects that “survived” the Eden space  Objects that are not garbage-collected by the Eden space collector Surviver Space is treated the same as Eden space and is also a Copy based garbage-collector Surviver Space is larger than Eden space, and is tuned to flush out the short-lived objects that “escaped” collection in Eden space

Surviver Space 2/2 Surviver Space is divided into 2 parts  From Space – Treated much the same as Eden space  To Space – Where objects that survive Eden, and From Space are allocated to When the To space is filled with objects  The To space is garbage collected  The remaining objects are promoted to the “Tenured Gen”

Tenured Gen 1/3 When Surviver To Space is filled up, the live objects are promoted to Tenured Gen Tenured Gen is collected with a “mark-sweep- compact” Garbage Collector Most “long lived” objects exist in Tenured space Garbage Collection of Tenured space causes a very short pause of execution to determine the “root” objects Tenured space is Garbage Collected by default when it is 68% full

Tenured Gen 2/3 The “mark-sweep-compact” Garbage Collector only pauses execution to find the “root” objects  Root objects are those that are directly accessible by one or more currently executing thread The Garbage Collector traverses the object graph from the root objects while execution continues  Objects that are traversed over are marked as active  Objects that are not marked will be garbage collected The Garbage Collector doesn't move objects during execution, so “holes” are left in the heap

Tenured Gen 3/3 When no more objects can be allocated into the Tenured Gen it does a “stop the world” collection  All threads are suspended  The Garbage Collector runs  Remaining objects are reshuffled to be at the bottom of the heap  Threads are re-started Tenured Gen is the largest space  It takes the longest to Garbage Collect  It is to large for a “copy” Garbage Collector  The “mark-sweep-compact” collector operates early

Perm Gen Perm Gen is the “permanent” space in the VM  Used to store class byte-codes  The Virtual Machine's “Main” thread  Reflection objects You can generally assume that Perm Gen is never Garbage Collected  This causes problems with ClassLoader s, since each ClassLoader may load it's own copy of a single class  The most visible symptom is an OutOfMemoryException from application servers

Garbage Collection Notes Primitives in Java are not references Local variable “slots” are re-used by the compiler to reduce the number of variables Java 1.6 further optimizes Garbage Collection by having the compiler hint at when to Garbage Collect certain variables (scoping) The Garbage Collectors are very well tuned to most situations, but can be tuned using options The JConsole gives a good overview of memory usage and can also monitor a remote VM

Garbage Collection End Questions and Coffee Break

Classloading Classloader Family Tree The Rules of Classloading Custom Classloading Breaking the Rules

Classloader Family Tree 1/2 Bootstrap Classloader is used to load the System ClassLoader  SystemClassLoader is used to load all other classes All Classes have a parent ClassLoader  Any Class accessed by the Class is requested from it's parent ClassLoader  The parent ClassLoader always loaded the class Each ClassLoader also has a parent ClassLoader, this is normally the parent ClassLoader of the Class that instantiated the ClassLoader

ClassLoader Family Tree 2/2 ClassLoader s can access the Classes of their parent ClassLoader, but not of their child ClassLoader s  Think about the relationship between a normal Class and it's parent ClassLoader System ClassLoader Custom ClassLoader Custom Child ClassLoader

The Rules of ClassLoading 1/2 Override the findClass method rather than loadClass If overriding loadClass : call super.loadClass before attempting to resolve the Class yourself  This ensures that “ java.* ” packages cannot be overridden  Also helps conserves memory by making sure that a minimal set of Classes are loaded at any given time ClassLoaders should also implement the findResource method for getClass().getResource

The Rules of ClassLoading 2/2 Classes are always loaded using the loadClass method The default implementation resolves the class by: 1.Invoke ClassLoader.findLoadedClass(String) to check if the class has already been loaded. 2.Invoke the loadClass method on the parent class loader. If the parent is null the class loader built-in to the virtual machine is used, instead. 3.Invoke the findClass(String) method to find the class.

Custom ClassLoading 1/2 public class CustomClassLoader extends ClassLoader { private File directory;... protected Class findClass(String name) throws ClassNotFoundException { String classFileName = name.replace('.', '/') + “.class”; File classFile = new File(directory, classFileName); if(!classFile.exists() || !classFile.isFile()) { throw new ClassNotFoundException(); } else { try { byte[] bytecode = new byte[(int)classFile.length()]; FileInputStream fin = new FileInputStream(classFile); int length = fin.read(bytecode); fin.close(); return defineClass(name, bytecode, 0, length); } catch(IOException ioe) { throw new ClassNotFoundException(“Error loading class”, ioe); }

Custom ClassLoading 2/2 As you can see, custom ClassLoading is very simple  Caching of already loaded class is done in loadClass  Finding alternative Class's is done in loadClass  Verification of bytecodes is done in defineClass  All you do is load the bytecode into an array and pass it to defineClass To access a class loaded with a custom ClassLoader, use loadClass(fullClassName) and normal reflection to construct the object

Breaking the Rules Breaking the ClassLoading rules allows you to override standard and already-loaded Class's To break the rules, you will generally override the loadClass(String, boolean) method of ClassLoader  Allows you to override standard API's Don't call super.loadClass  Allows you to overwrite already loaded classes Don't make a call to findLoadedClass The old bytecode will exist so long as there is still a reference to an object of the old type

Questions If there are any?