SlideShare a Scribd company logo
1 of 55
Download to read offline
JVM Internals
         Douglas Q. Hawkins




Sunday, August 15, 2010
JVM Internals

               Bytecode
               Garbage Collection
               Optimizations
                     Compile Time
                     Run Time




Sunday, August 15, 2010
Java Bytecode




Sunday, August 15, 2010
Java Bytecode
                                      Local Variables
               Stack Based
                                      Operand Stack
               Local Variable Space




Sunday, August 15, 2010
Java Bytecode
                                      Local Variables
               Stack Based
                                      Operand Stack
               Local Variable Space




                                            3

Sunday, August 15, 2010
Java Bytecode
                                      Local Variables
               Stack Based
                                      Operand Stack
               Local Variable Space




                                            7
                                            3

Sunday, August 15, 2010
Java Bytecode
                                      Local Variables
               Stack Based
                                      Operand Stack
               Local Variable Space




                                            7           +
                                            3

Sunday, August 15, 2010
Java Bytecode
                                      Local Variables
               Stack Based
                                      Operand Stack
               Local Variable Space




                                                    3+ 7


Sunday, August 15, 2010
Java Bytecode
                                      Local Variables
               Stack Based
                                      Operand Stack
               Local Variable Space




                                            10

Sunday, August 15, 2010
Operation Types
               Load and Store
               Arithmetic and Logic
               Type Conversion
               Control Transfer
               Object Creation and Manipulation
               Operand Stack
               Method Invocation


Sunday, August 15, 2010
Demo




Sunday, August 15, 2010
Garbage Collection




Sunday, August 15, 2010
Garbage Collection
               Generational Garbage Collection
                     Segmented into Young, Old, and Permanent
                     Generations


               Types of Collectors
                     Parallel - across multiple threads
                     Concurrent - while program runs


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden

           Survivor
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden      A   B

           Survivor
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden      A   B    C   D

           Survivor
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden      A   B    C   D   E   F   G

           Survivor
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden      A   B    C   D   E   F   G

           Survivor
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden      A        C   D   G

           Survivor
                          B   E    F
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden

           Survivor
                          B   E    F
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden      H    I   J   K   L   M   N

           Survivor
                          B   E    F
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden      H    I   J   K   L   M   N

           Survivor
                          B   E    F
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden      H    I       K   L   M   N

           Survivor
                                   F       B   E   J
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden

           Survivor
                                   B   E   J
           Spaces
                  Old Generation
           Tenured


Sunday, August 15, 2010
Generational Garbage
         Collection
              Young Generation

                Eden

           Survivor
           Spaces
                  Old Generation
           Tenured        B   E    J


Sunday, August 15, 2010
Demo




Sunday, August 15, 2010
Garbage Collection Pattern
               Minor
               Major
               Major Again - for objects with finalize
               Soft References
               Major
               Major Again - for objects with finalize
               Throw OutOfMemoryError

Sunday, August 15, 2010
Optimizations




Sunday, August 15, 2010
Optimizations
               Just In Time Compilation
                     Purely Interpreted
                     Ahead of Time Compilation


               Almost No Compile Time Optimization
               Most Optimizations are Runtime

Sunday, August 15, 2010
Compile Time Demo




Sunday, August 15, 2010
Is This Optimized?
         double sumU = 0, sumV = 0;
         for ( int i = 0; i < 100; ++i ) {
           Vector2D vector = new Vector2D( i, i );
           synchronized ( vector ) {
              sumU += vector.getU();
              sumV += vector.getV();
           }
         }



Sunday, August 15, 2010
Is This Optimized?
         double sumU = 0, sumV = 0;
         for ( int i = 0; i < 100; ++i ) {
           Vector2D vector = new Vector2D( i, i );
           synchronized ( vector ) {
              sumU += vector.getU(); How many...?
              sumV += vector.getV(); Loop Iterations
           }
                                           Heap Allocations
         }
                                          Method Invocations
                                          Lock Operations

Sunday, August 15, 2010
Is This Optimized?
         double sumU = 0, sumV = 0;
         for ( int i = 0; i < 100; ++i ) {
           Vector2D vector = new Vector2D( i, i );
           synchronized ( vector ) {
              sumU += vector.getU(); How many...?
              sumV += vector.getV(); Loop Iterations          100
           }
                                           Heap Allocations   100
         }
                                          Method Invocations 200
                                          Lock Operations     100

Sunday, August 15, 2010
Is This Optimized?
         double sumU = 0, sumV = 0;
         for ( int i = 0; i < 100; ++i ) {
           Vector2D vector = new Vector2D( i, i );
           synchronized ( vector ) {
              sumU += vector.getU(); How many...?
              sumV += vector.getV(); Loop Iterations           0
           }
                                           Heap Allocations    0
         }
                                          Method Invocations   0
                                          Lock Operations      0

Sunday, August 15, 2010
Common Sub-Expression
         Elimination
            int x = a + b;
            int y = a + b;




Sunday, August 15, 2010
Common Sub-Expression
         Elimination
            int x = a + b;
            int y = a + b;




            int tmp = a + b;
            int x = tmp;
            int y = tmp;


Sunday, August 15, 2010
Array Bounds Check Elimination
            int[] nums = ...
            for ( int i = 0; i < nums.length; ++i ) {
              System.out.println( “nums[“ + i + “]=” + nums[ i ] );
            }




Sunday, August 15, 2010
Array Bounds Check Elimination
            int[] nums = ...
            for ( int i = 0; i < nums.length; ++i ) {
              System.out.println( “nums[“ + i + “]=” + nums[ i ] );
            }


           int[] nums = ...
           for ( int i = 0; i < nums.length; ++i ) {
             if ( i < 0 || i >= nums.length ) {
                  throw new ArrayIndexOutOfBoundsException();
             }
             System.out.println( “nums[“ + i + “]=” + nums[ i ] );
           }

Sunday, August 15, 2010
Loop Invariant Hoisting
            for ( int i = 0; i < nums.length; ++i ) {
            ...
            }




Sunday, August 15, 2010
Loop Invariant Hoisting
            for ( int i = 0; i < nums.length; ++i ) {
            ...
            }


            int length = nums.length;
            for ( int i = 0; i < length; ++i ) {
            ...
            }

Sunday, August 15, 2010
Loop Unrolling
            int sum = 0;
            for ( int i = 0; i < 10; ++i ) {
              sum += i;
            }




Sunday, August 15, 2010
Loop Unrolling
            int sum = 0;
            for ( int i = 0; i < 10; ++i ) {
              sum += i;
            }

            int sum = 0;
            sum += 1;
            ...
            sum += 9;

Sunday, August 15, 2010
Method Inlining
            Vector vector = ...
            double magnitude = vector.magnitude();




Sunday, August 15, 2010
Method Inlining
            Vector vector = ...
            double magnitude = vector.magnitude();

           Vector vector = ...
           double magnitude = Math.sqrt(
            vector.u*vector.u + vector.v*vector.v );




Sunday, August 15, 2010
Method Inlining
            Vector vector = ...
            double magnitude = vector.magnitude();

           Vector vector = ...
           double magnitude = Math.sqrt(
            vector.u*vector.u + vector.v*vector.v );

            Vector vector = ...
            double magnitude;
            if ( vector instance of Vector2D ) {
              magnitude = Math.sqrt(
                 vector.u*vector.u + vector.v*vector.v );
            } else {
              magnitude = vector.magnitude();
            }
Sunday, August 15, 2010
Method Inlining
            Vector vector = ...
            double magnitude = vector.magnitude();
                                                      static      always
           Vector vector = ...
                                                      final        always
           double magnitude = Math.sqrt(
             vector.u*vector.u + vector.v*vector.v ); private     always
                                                      virtual     often
           Vector vector = ...
                                                      reflective   sometimes
           double magnitude;
           if ( vector instance of Vector2D ) {       dynamic     often
             magnitude = Math.sqrt(
                vector.u*vector.u + vector.v*vector.v );
           } else {
             magnitude = vector.magnitude();
           }
Sunday, August 15, 2010
Lock Coarsening
            StringBuffer buffer = ...
            buffer.append( “Hello” );
            buffer.append( name );
            buffer.append( “n” );




Sunday, August 15, 2010
Lock Coarsening
            StringBuffer buffer = ...
            buffer.append( “Hello” );
            buffer.append( name );
            buffer.append( “n” );

            StringBuffer buffer = ...
            lock( buffer ); buffer.append( “Hello” ); unlock( buffer );
            lock( buffer ); buffer.append( name ); unlock( buffer );
            lock( buffer ); buffer.append( “n” ); unlock( buffer );




Sunday, August 15, 2010
Lock Coarsening
            StringBuffer buffer = ...
            buffer.append( “Hello” );
            buffer.append( name );
            buffer.append( “n” );

            StringBuffer buffer = ...
            lock( buffer ); buffer.append( “Hello” ); unlock( buffer );
            lock( buffer ); buffer.append( name ); unlock( buffer );
            lock( buffer ); buffer.append( “n” ); unlock( buffer );

           StringBuffer buffer = ...
           lock( buffer );
           buffer.append( “Hello” );
           buffer.append( name );
           buffer.append( “n” );
           unlock( buffer );
Sunday, August 15, 2010
Other Lock Optimizations
               Biased Locking
               Adaptive Locking - Thread sleep vs. Spin lock




Sunday, August 15, 2010
Escape Analysis
           Point p1 = new Point( x1, y1 ), p2 = new Point( x2, y2 );
           synchronized ( p1 ) {
             synchronized ( p2 ) {
               double dx = p1.getX() - p2.getX();
               double dy = p1.getY() - p2.getY();
               double distance = Math.sqrt( dx*dx + dy*dy );
             }
           }




Sunday, August 15, 2010
Escape Analysis
           Point p1 = new Point( x1, y1 ), p2 = new Point( x2, y2 );
           double dx = p1.getX() - p2.getX();
           double dy = p1.getY() - p2.getY();
           double distance = Math.sqrt( dx*dx + dy*dy );




Sunday, August 15, 2010
Escape Analysis
           Point p1 = new Point( x1, y1 ), p2 = new Point( x2, y2 );
           double dx = p1.getX() - p2.getX();
           double dy = p1.getY() - p2.getY();
           double distance = Math.sqrt( dx*dx + dy*dy );




            double dx = x1 - x2;
            double dx = y1 - y2;
            double distance = Math.sqrt( dx*dx + dy*dy );


Sunday, August 15, 2010
Run Time Demo




Sunday, August 15, 2010
Resources
               Brian Goetz

                     Developer Works Articles

               Tony Printezis

                     Garbage Collection in the Java HotSpot Virtual Machine - http://www.devx.com/
                     Java/Article/21977

               Java Specialist Newsletter - http://www.javaspecialists.eu/

               http://java.sun.com/javase/6/docs/technotes/guides/vm/cms-6.html

               http://java.sun.com/docs/hotspot/gc1.4.2/faq.html

               http://www.fasterj.com/articles/G1.html

               http://www.informit.com/guides/content.aspx?g=java&seqNum=27



Sunday, August 15, 2010

More Related Content

More from Doug Hawkins

JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksDoug Hawkins
 
ReadyNow: Azul's Unconventional "AOT"
ReadyNow: Azul's Unconventional "AOT"ReadyNow: Azul's Unconventional "AOT"
ReadyNow: Azul's Unconventional "AOT"Doug Hawkins
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance PuzzlersDoug Hawkins
 
Concurrency Concepts in Java
Concurrency Concepts in JavaConcurrency Concepts in Java
Concurrency Concepts in JavaDoug Hawkins
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
JVM Internals - NHJUG Jan 2012
JVM Internals - NHJUG Jan 2012JVM Internals - NHJUG Jan 2012
JVM Internals - NHJUG Jan 2012Doug Hawkins
 
Inside Android's Dalvik VM - NEJUG Nov 2011
Inside Android's Dalvik VM - NEJUG Nov 2011Inside Android's Dalvik VM - NEJUG Nov 2011
Inside Android's Dalvik VM - NEJUG Nov 2011Doug Hawkins
 
JVM Internals - NEJUG Nov 2010
JVM Internals - NEJUG Nov 2010JVM Internals - NEJUG Nov 2010
JVM Internals - NEJUG Nov 2010Doug Hawkins
 
Introduction to Class File Format & Byte Code
Introduction to Class File Format & Byte CodeIntroduction to Class File Format & Byte Code
Introduction to Class File Format & Byte CodeDoug Hawkins
 

More from Doug Hawkins (10)

JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's Tricks
 
ReadyNow: Azul's Unconventional "AOT"
ReadyNow: Azul's Unconventional "AOT"ReadyNow: Azul's Unconventional "AOT"
ReadyNow: Azul's Unconventional "AOT"
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance Puzzlers
 
JVM Mechanics
JVM MechanicsJVM Mechanics
JVM Mechanics
 
Concurrency Concepts in Java
Concurrency Concepts in JavaConcurrency Concepts in Java
Concurrency Concepts in Java
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
JVM Internals - NHJUG Jan 2012
JVM Internals - NHJUG Jan 2012JVM Internals - NHJUG Jan 2012
JVM Internals - NHJUG Jan 2012
 
Inside Android's Dalvik VM - NEJUG Nov 2011
Inside Android's Dalvik VM - NEJUG Nov 2011Inside Android's Dalvik VM - NEJUG Nov 2011
Inside Android's Dalvik VM - NEJUG Nov 2011
 
JVM Internals - NEJUG Nov 2010
JVM Internals - NEJUG Nov 2010JVM Internals - NEJUG Nov 2010
JVM Internals - NEJUG Nov 2010
 
Introduction to Class File Format & Byte Code
Introduction to Class File Format & Byte CodeIntroduction to Class File Format & Byte Code
Introduction to Class File Format & Byte Code
 

Recently uploaded

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
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 AutomationSafe Software
 
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?Igalia
 
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...Martijn de Jong
 
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...Drew Madelung
 
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.pdfUK Journal
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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...Neo4j
 
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 RobisonAnna Loughnan Colquhoun
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Recently uploaded (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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 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?
 
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...
 
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...
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
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...
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

JVM Internals - Garbage Collection & Runtime Optimizations

  • 1. JVM Internals Douglas Q. Hawkins Sunday, August 15, 2010
  • 2. JVM Internals Bytecode Garbage Collection Optimizations Compile Time Run Time Sunday, August 15, 2010
  • 4. Java Bytecode Local Variables Stack Based Operand Stack Local Variable Space Sunday, August 15, 2010
  • 5. Java Bytecode Local Variables Stack Based Operand Stack Local Variable Space 3 Sunday, August 15, 2010
  • 6. Java Bytecode Local Variables Stack Based Operand Stack Local Variable Space 7 3 Sunday, August 15, 2010
  • 7. Java Bytecode Local Variables Stack Based Operand Stack Local Variable Space 7 + 3 Sunday, August 15, 2010
  • 8. Java Bytecode Local Variables Stack Based Operand Stack Local Variable Space 3+ 7 Sunday, August 15, 2010
  • 9. Java Bytecode Local Variables Stack Based Operand Stack Local Variable Space 10 Sunday, August 15, 2010
  • 10. Operation Types Load and Store Arithmetic and Logic Type Conversion Control Transfer Object Creation and Manipulation Operand Stack Method Invocation Sunday, August 15, 2010
  • 13. Garbage Collection Generational Garbage Collection Segmented into Young, Old, and Permanent Generations Types of Collectors Parallel - across multiple threads Concurrent - while program runs Sunday, August 15, 2010
  • 14. Generational Garbage Collection Young Generation Eden Survivor Spaces Old Generation Tenured Sunday, August 15, 2010
  • 15. Generational Garbage Collection Young Generation Eden A B Survivor Spaces Old Generation Tenured Sunday, August 15, 2010
  • 16. Generational Garbage Collection Young Generation Eden A B C D Survivor Spaces Old Generation Tenured Sunday, August 15, 2010
  • 17. Generational Garbage Collection Young Generation Eden A B C D E F G Survivor Spaces Old Generation Tenured Sunday, August 15, 2010
  • 18. Generational Garbage Collection Young Generation Eden A B C D E F G Survivor Spaces Old Generation Tenured Sunday, August 15, 2010
  • 19. Generational Garbage Collection Young Generation Eden A C D G Survivor B E F Spaces Old Generation Tenured Sunday, August 15, 2010
  • 20. Generational Garbage Collection Young Generation Eden Survivor B E F Spaces Old Generation Tenured Sunday, August 15, 2010
  • 21. Generational Garbage Collection Young Generation Eden H I J K L M N Survivor B E F Spaces Old Generation Tenured Sunday, August 15, 2010
  • 22. Generational Garbage Collection Young Generation Eden H I J K L M N Survivor B E F Spaces Old Generation Tenured Sunday, August 15, 2010
  • 23. Generational Garbage Collection Young Generation Eden H I K L M N Survivor F B E J Spaces Old Generation Tenured Sunday, August 15, 2010
  • 24. Generational Garbage Collection Young Generation Eden Survivor B E J Spaces Old Generation Tenured Sunday, August 15, 2010
  • 25. Generational Garbage Collection Young Generation Eden Survivor Spaces Old Generation Tenured B E J Sunday, August 15, 2010
  • 27. Garbage Collection Pattern Minor Major Major Again - for objects with finalize Soft References Major Major Again - for objects with finalize Throw OutOfMemoryError Sunday, August 15, 2010
  • 29. Optimizations Just In Time Compilation Purely Interpreted Ahead of Time Compilation Almost No Compile Time Optimization Most Optimizations are Runtime Sunday, August 15, 2010
  • 30. Compile Time Demo Sunday, August 15, 2010
  • 31. Is This Optimized? double sumU = 0, sumV = 0; for ( int i = 0; i < 100; ++i ) { Vector2D vector = new Vector2D( i, i ); synchronized ( vector ) { sumU += vector.getU(); sumV += vector.getV(); } } Sunday, August 15, 2010
  • 32. Is This Optimized? double sumU = 0, sumV = 0; for ( int i = 0; i < 100; ++i ) { Vector2D vector = new Vector2D( i, i ); synchronized ( vector ) { sumU += vector.getU(); How many...? sumV += vector.getV(); Loop Iterations } Heap Allocations } Method Invocations Lock Operations Sunday, August 15, 2010
  • 33. Is This Optimized? double sumU = 0, sumV = 0; for ( int i = 0; i < 100; ++i ) { Vector2D vector = new Vector2D( i, i ); synchronized ( vector ) { sumU += vector.getU(); How many...? sumV += vector.getV(); Loop Iterations 100 } Heap Allocations 100 } Method Invocations 200 Lock Operations 100 Sunday, August 15, 2010
  • 34. Is This Optimized? double sumU = 0, sumV = 0; for ( int i = 0; i < 100; ++i ) { Vector2D vector = new Vector2D( i, i ); synchronized ( vector ) { sumU += vector.getU(); How many...? sumV += vector.getV(); Loop Iterations 0 } Heap Allocations 0 } Method Invocations 0 Lock Operations 0 Sunday, August 15, 2010
  • 35. Common Sub-Expression Elimination int x = a + b; int y = a + b; Sunday, August 15, 2010
  • 36. Common Sub-Expression Elimination int x = a + b; int y = a + b; int tmp = a + b; int x = tmp; int y = tmp; Sunday, August 15, 2010
  • 37. Array Bounds Check Elimination int[] nums = ... for ( int i = 0; i < nums.length; ++i ) { System.out.println( “nums[“ + i + “]=” + nums[ i ] ); } Sunday, August 15, 2010
  • 38. Array Bounds Check Elimination int[] nums = ... for ( int i = 0; i < nums.length; ++i ) { System.out.println( “nums[“ + i + “]=” + nums[ i ] ); } int[] nums = ... for ( int i = 0; i < nums.length; ++i ) { if ( i < 0 || i >= nums.length ) { throw new ArrayIndexOutOfBoundsException(); } System.out.println( “nums[“ + i + “]=” + nums[ i ] ); } Sunday, August 15, 2010
  • 39. Loop Invariant Hoisting for ( int i = 0; i < nums.length; ++i ) { ... } Sunday, August 15, 2010
  • 40. Loop Invariant Hoisting for ( int i = 0; i < nums.length; ++i ) { ... } int length = nums.length; for ( int i = 0; i < length; ++i ) { ... } Sunday, August 15, 2010
  • 41. Loop Unrolling int sum = 0; for ( int i = 0; i < 10; ++i ) { sum += i; } Sunday, August 15, 2010
  • 42. Loop Unrolling int sum = 0; for ( int i = 0; i < 10; ++i ) { sum += i; } int sum = 0; sum += 1; ... sum += 9; Sunday, August 15, 2010
  • 43. Method Inlining Vector vector = ... double magnitude = vector.magnitude(); Sunday, August 15, 2010
  • 44. Method Inlining Vector vector = ... double magnitude = vector.magnitude(); Vector vector = ... double magnitude = Math.sqrt( vector.u*vector.u + vector.v*vector.v ); Sunday, August 15, 2010
  • 45. Method Inlining Vector vector = ... double magnitude = vector.magnitude(); Vector vector = ... double magnitude = Math.sqrt( vector.u*vector.u + vector.v*vector.v ); Vector vector = ... double magnitude; if ( vector instance of Vector2D ) { magnitude = Math.sqrt( vector.u*vector.u + vector.v*vector.v ); } else { magnitude = vector.magnitude(); } Sunday, August 15, 2010
  • 46. Method Inlining Vector vector = ... double magnitude = vector.magnitude(); static always Vector vector = ... final always double magnitude = Math.sqrt( vector.u*vector.u + vector.v*vector.v ); private always virtual often Vector vector = ... reflective sometimes double magnitude; if ( vector instance of Vector2D ) { dynamic often magnitude = Math.sqrt( vector.u*vector.u + vector.v*vector.v ); } else { magnitude = vector.magnitude(); } Sunday, August 15, 2010
  • 47. Lock Coarsening StringBuffer buffer = ... buffer.append( “Hello” ); buffer.append( name ); buffer.append( “n” ); Sunday, August 15, 2010
  • 48. Lock Coarsening StringBuffer buffer = ... buffer.append( “Hello” ); buffer.append( name ); buffer.append( “n” ); StringBuffer buffer = ... lock( buffer ); buffer.append( “Hello” ); unlock( buffer ); lock( buffer ); buffer.append( name ); unlock( buffer ); lock( buffer ); buffer.append( “n” ); unlock( buffer ); Sunday, August 15, 2010
  • 49. Lock Coarsening StringBuffer buffer = ... buffer.append( “Hello” ); buffer.append( name ); buffer.append( “n” ); StringBuffer buffer = ... lock( buffer ); buffer.append( “Hello” ); unlock( buffer ); lock( buffer ); buffer.append( name ); unlock( buffer ); lock( buffer ); buffer.append( “n” ); unlock( buffer ); StringBuffer buffer = ... lock( buffer ); buffer.append( “Hello” ); buffer.append( name ); buffer.append( “n” ); unlock( buffer ); Sunday, August 15, 2010
  • 50. Other Lock Optimizations Biased Locking Adaptive Locking - Thread sleep vs. Spin lock Sunday, August 15, 2010
  • 51. Escape Analysis Point p1 = new Point( x1, y1 ), p2 = new Point( x2, y2 ); synchronized ( p1 ) { synchronized ( p2 ) { double dx = p1.getX() - p2.getX(); double dy = p1.getY() - p2.getY(); double distance = Math.sqrt( dx*dx + dy*dy ); } } Sunday, August 15, 2010
  • 52. Escape Analysis Point p1 = new Point( x1, y1 ), p2 = new Point( x2, y2 ); double dx = p1.getX() - p2.getX(); double dy = p1.getY() - p2.getY(); double distance = Math.sqrt( dx*dx + dy*dy ); Sunday, August 15, 2010
  • 53. Escape Analysis Point p1 = new Point( x1, y1 ), p2 = new Point( x2, y2 ); double dx = p1.getX() - p2.getX(); double dy = p1.getY() - p2.getY(); double distance = Math.sqrt( dx*dx + dy*dy ); double dx = x1 - x2; double dx = y1 - y2; double distance = Math.sqrt( dx*dx + dy*dy ); Sunday, August 15, 2010
  • 54. Run Time Demo Sunday, August 15, 2010
  • 55. Resources Brian Goetz Developer Works Articles Tony Printezis Garbage Collection in the Java HotSpot Virtual Machine - http://www.devx.com/ Java/Article/21977 Java Specialist Newsletter - http://www.javaspecialists.eu/ http://java.sun.com/javase/6/docs/technotes/guides/vm/cms-6.html http://java.sun.com/docs/hotspot/gc1.4.2/faq.html http://www.fasterj.com/articles/G1.html http://www.informit.com/guides/content.aspx?g=java&seqNum=27 Sunday, August 15, 2010