SlideShare une entreprise Scribd logo
1  sur  39
Télécharger pour lire hors ligne
PUBLIC
Thomas Stüfe, SAP
Feb 01, 2020
Taming Metaspace
© 2019 SAP SE or an SAP affiliate company. All rights reserved.
No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of
SAP SE or an SAP affiliate company.
The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its
distributors contain proprietary software components of other software vendors. National product specifications may vary.
These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or
warranty of any kind, and SAP or its affiliated companies shall not be liable for errors or omissions with respect to the materials.
The only warranties for SAP or SAP affiliate company products and services are those that are set forth in the express warranty
statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional
warranty.
In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or
any related presentation, or to develop or release any functionality mentioned therein. This document, or any related presentation,
and SAP SE’s or its affiliated companies’ strategy and possible future developments, products, and/or platforms, directions, and
functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason
without notice. The information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or
functionality. All forward-looking statements are subject to various risks and uncertainties that could cause actual results to differ
materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements, and they
should not be relied upon in making purchasing decisions.
SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered
trademarks of SAP SE (or an SAP affiliate company) in Germany and other countries. All other product and service names
mentioned are the trademarks of their respective companies.
See www.sap.com/copyright for additional trademark information and notices.
www.sap.com/contactsap
Follow us
3PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
(Taken from
https://spring.io/blog/2019/03/11/memory-footprint-of-the-jvm
Note: Metaspace is mislabeled as Compressed Class Space)
Metaspace can consume a lot of memory.
Can we improve that? Yes, within reason.
Why do we care?
Basics
6PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
 Metaspace contains class metadata
– Klass, Constant Pool, Method, Annotations, Bytecode, JIT Counters etc.
 Used to live in Java Heap (Permanent Generation) pre JDK 8
 JDK 8: PermGen Removal -> Metaspace is born
– Inspired by JRockit VM
– JEP 122: “JEP 122: Remove the Permanent Generation”
 SAP involvement:
– JDK 11: partial rework (chunk coalescation, JDK-8198423)
– Analysis tools: jcmd VM.metaspace, VM.classloaders
– many smaller fixes/cleanups
– JDK 15 (?): rewrite
Metaspace
7PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
 Metadata are usually allocated when classes are loaded
 All metadata a loader accumulated is freed in bulk after the loader has been collected
– Exception: Metadata may be deallocated earlier (class redefinition, load errors etc) but that’s uncommon.
– Deallocated space still belongs to the loader.
Metadata lifecycle
8PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
 Bulk delete allows arena style allocation!
– No need to track individual allocations
– Simple pointer bump allocation possible: cheap and allows tight packing
 We know the size distribution of typical allocations
 Case against malloc in particular:
– CompressedClassSpace
– Platform specific limitations (e.g. sbrk hits java heap)
Why write a customized allocator?
9PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
 Compressed Class Space is a (small) part of Metaspace
 Optimization for 64bit platforms
 Only on 64bit, if –XX:+UseCompressedClassPointers (on by default)
Compressed Class Space
10PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Compressed Class Space
Java object Klass
vtable
itable
…
Mark word
64bit Klass*
Heap Metaspace
11PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Compressed Class Space
Java object Klass
vtable
itable
…
Mark word
32bit
index
base
Compressed Class Space
Heap
Shared classes
Klass* = (base + 32 bit index) [<< 3]
12PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Metaspace has two parts
„Compressed“ class space
D1 „non class“ Metaspace
Commit HWM
K
1
…D2 D2
K
2
Sizes per class:
• ~1K Klass (500+ … 500K)
• ~6K non-class (~2K … xxK)
Klass
structures
Everything
else
-XX:CompressedClassSpaceSize
Current implementation
16PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Current implementation (1)
(much simplified)
Loader
top
• Loader owns a chunk of memory.
• Allocates from it via pointer bump.
• Remember: we do not need to track individual allocations for freeing.
17PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Current implementation (2)
(much simplified)
Loader
• If chunk is used up, Loader aquires a new one from the metaspace allocator.
• Retired chunks are kept in list
• Leftover space is kept for later reuse
Current chunk
„Retired chunks“
leftover space
18PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Current implementation (3)
(much simplified)
Used
Reserved
Loader A
Loader B
Loader C
C
A B A A A
C B B A
A B C AB
B
Committed
A A A A A A A
B B B B B
C C C
Chunks are carved from metaspace memory as they
are allocated.
GC Threshold
19PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Current implementation (4)
(much simplified)
Loader A
Loader B
Loader C
A A A A A A A
B B B B B
C C C
When a loader dies, its chunks are marked as free…
Used
Reserved
C
A B A A A
C B B A
A B C AB
B
Committed
GC Threshold
20PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Current implementation (5)
(very much simplified)
Loader A
Loader C
A A A A A A A
C C C
…and added to global freelists, sorted by size.
Chunk Freelists
Used
Reserved
C
A A A A
C A
A C A
Committed
GC Threshold
21PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
 Freelists can get huge.
– We have seen used:free ratios of 1:3 and worse
– =>Metaspace is not really elastic.
 Intra-chunk waste
– At some point loader typically stops loading classes; remaining chunk space is wasted
– Worse with many tiny loaders (reflection delegator classes, lambda anonymous classes)
 Code bloat
– Expensive to maintain.
– Code base grew over time and has gotten overly complicated.
Problems with the current implementation
22PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Huge freelists: Committed vs used space, after class unloading
~270M

23PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
jcmd 27265 VM.metaspace
27265:
…
Waste (percentages refer to total committed size 373,48 MB):
Committed unused: 280,00 KB ( <1%)
Waste in chunks in use: 2,45 KB ( <1%)
Free in chunks in use: 6,34 MB ( 2%)
Overhead in chunks in use: 186,75 KB ( <1%)
In free chunks: 269,56 MB ( 72%)
Deallocated from chunks in use: 998,98 KB ( <1%) (1763 blocks)
-total-: 277,33 MB ( 74%)
Huge Freelists (jcmd VM.metaspace output)
Reimplementation
26PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
 Uncommit chunks in freelists
●
Makes Metaspace more elastic
 Delay committing chunks until they are actually used
●
Loader commits as needed (like a thread stack)
●
Removes the penalty of handing out large chunks to class loaders
●
Mitigates intrachunk waste problem (at least for large chunks)
Basic idea
27PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
 (Linux): we decommit with mmap(MAP_NORESERVE) && mprotect(PROT_NONE)
– Higher commit/uncommit fragmentation results in higher number of VMAs
– Kernel keeps vma structures in list and rb tree
– Too many of them may affect vma lookup
– And we may hit process limits
 So: keep an eye on commit granularity
Concern: keep number of virtual memory areas low
28PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Commit granules
Before:
commit HWM
Now:
Granule size
Adjustable, defaults to 64k
29PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
 Odd chunk geometry
– Difficult to merge and split
– High fragmentation
– Complex code
 Chunk headers are a problem
Current chunk allocation scheme unsuited for uncommitting chunks
30PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
• Power 2 based buddy allocation scheme
• Chunks sized from 1K … 4M in pow2 steps
• Dead simple to split and merge.
• Low external defragmentation -> Leads to larger free contiguous areas.
• Standard algorithm widely known
Pow 2 based buddy allocator for chunks
32PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Buddy allocator: Deallocation
 Mark chunk as free
 If buddy chunk is free and unsplit: remove from freelist and merge with chunk
– Repeat until root chunk sized reached or until buddy is not free
 Return result chunk to free list
64K 256K128K64K 64K 256K64K 64K 64K
512K 64K 256K64K 64K 64K
33PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Chunk headers needed to go
page page page page
ChunkHeaderPool
page page page page
Header
Before
Now
34PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Granules and chunks
Chunks
Granules
256K 64K
32
K
1
6
K
1
6
K
128K
 A larger chunk can span multiple granules (1:n)
 Multiple small chunks can cover a single granule (n:1)
1:n n:1
35PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Granules and chunks
Chunks
Granules
256K 64K
32
K
1
6
K
1
6
K
128K
 Free chunks spanning 1+ granules can be uncommitted
 A chunk spanning >1 granules can be committed on demand
36PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
• Got rid of humongous chunks :)
• Got rid of occupancy map
• Better deallocation management
• Chunks can now often grow in-place
• Saves overhead and reduces intrachunk waste
• Code is cleaner and more maintainable; better separation of concerns and testability.
What else changed
37PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Result: Committed vs used, Stock JDK14
~270M

38PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Result: Committed vs used, Patched JDK14
39PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Result: committed Metaspace, Stock vs Patched VM
40PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Result: RSS, Stock vs Patched VM
41PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
Bottomline: Results
 Metaspace is now elastic. Freelist problem solved (almost).
 Modest decrease in consumption even without mass class unloading
●
Wildfly standalone after startup: 61m->54m, -7m, (11%)
●
Eclipse CDT, hotspot project after C++ indexing: 138m->129m, -9m (12%)
●
jruby helloworld.rb (invokedynamic, compile=FORCE): 41m->38m, -3m, (1.2%)
●
→ But, still more ideas and room for improvement.
 Better overall code quality
●
Better separation of concerns, better testability, more generic coding
42PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ
How do we go from here?
 Patch is stable. Needs more tests and may need smaller fixes but it works.
 Patch lives in jdk/sandbox repository, branch "stuefe-new-metaspace-branch“
– http://hg.openjdk.java.net/jdk/sandbox/
 JEP exists in Draft state (“Elastic Metaspace”: https://openjdk.java.net/jeps/8221173 )
 JDK15?
– Very difficult to bring such a large patch upstream
 A good candidate for backporting!
– Would make a lot of sense in 11/8
– Large patch but Metaspace is quite isolated. Should not be too much of a hassle.
Contact information:
Thomas Stüfe
@tstuefe
thomas.stuefe@sap.com
stuefe.de
Thank you.
Q/A

Contenu connexe

Dernier

Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Dernier (20)

Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 

En vedette

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

En vedette (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Taming Metaspace: a look at the machinery, and a proposal for a better one | FOSDEM 2020

  • 1. PUBLIC Thomas Stüfe, SAP Feb 01, 2020 Taming Metaspace
  • 2. © 2019 SAP SE or an SAP affiliate company. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company. The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components of other software vendors. National product specifications may vary. These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated companies shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty. In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release any functionality mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products, and/or platforms, directions, and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forward-looking statements are subject to various risks and uncertainties that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements, and they should not be relied upon in making purchasing decisions. SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other countries. All other product and service names mentioned are the trademarks of their respective companies. See www.sap.com/copyright for additional trademark information and notices. www.sap.com/contactsap Follow us
  • 3. 3PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ (Taken from https://spring.io/blog/2019/03/11/memory-footprint-of-the-jvm Note: Metaspace is mislabeled as Compressed Class Space) Metaspace can consume a lot of memory. Can we improve that? Yes, within reason. Why do we care?
  • 5. 6PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ  Metaspace contains class metadata – Klass, Constant Pool, Method, Annotations, Bytecode, JIT Counters etc.  Used to live in Java Heap (Permanent Generation) pre JDK 8  JDK 8: PermGen Removal -> Metaspace is born – Inspired by JRockit VM – JEP 122: “JEP 122: Remove the Permanent Generation”  SAP involvement: – JDK 11: partial rework (chunk coalescation, JDK-8198423) – Analysis tools: jcmd VM.metaspace, VM.classloaders – many smaller fixes/cleanups – JDK 15 (?): rewrite Metaspace
  • 6. 7PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ  Metadata are usually allocated when classes are loaded  All metadata a loader accumulated is freed in bulk after the loader has been collected – Exception: Metadata may be deallocated earlier (class redefinition, load errors etc) but that’s uncommon. – Deallocated space still belongs to the loader. Metadata lifecycle
  • 7. 8PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ  Bulk delete allows arena style allocation! – No need to track individual allocations – Simple pointer bump allocation possible: cheap and allows tight packing  We know the size distribution of typical allocations  Case against malloc in particular: – CompressedClassSpace – Platform specific limitations (e.g. sbrk hits java heap) Why write a customized allocator?
  • 8. 9PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ  Compressed Class Space is a (small) part of Metaspace  Optimization for 64bit platforms  Only on 64bit, if –XX:+UseCompressedClassPointers (on by default) Compressed Class Space
  • 9. 10PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Compressed Class Space Java object Klass vtable itable … Mark word 64bit Klass* Heap Metaspace
  • 10. 11PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Compressed Class Space Java object Klass vtable itable … Mark word 32bit index base Compressed Class Space Heap Shared classes Klass* = (base + 32 bit index) [<< 3]
  • 11. 12PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Metaspace has two parts „Compressed“ class space D1 „non class“ Metaspace Commit HWM K 1 …D2 D2 K 2 Sizes per class: • ~1K Klass (500+ … 500K) • ~6K non-class (~2K … xxK) Klass structures Everything else -XX:CompressedClassSpaceSize
  • 13. 16PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Current implementation (1) (much simplified) Loader top • Loader owns a chunk of memory. • Allocates from it via pointer bump. • Remember: we do not need to track individual allocations for freeing.
  • 14. 17PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Current implementation (2) (much simplified) Loader • If chunk is used up, Loader aquires a new one from the metaspace allocator. • Retired chunks are kept in list • Leftover space is kept for later reuse Current chunk „Retired chunks“ leftover space
  • 15. 18PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Current implementation (3) (much simplified) Used Reserved Loader A Loader B Loader C C A B A A A C B B A A B C AB B Committed A A A A A A A B B B B B C C C Chunks are carved from metaspace memory as they are allocated. GC Threshold
  • 16. 19PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Current implementation (4) (much simplified) Loader A Loader B Loader C A A A A A A A B B B B B C C C When a loader dies, its chunks are marked as free… Used Reserved C A B A A A C B B A A B C AB B Committed GC Threshold
  • 17. 20PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Current implementation (5) (very much simplified) Loader A Loader C A A A A A A A C C C …and added to global freelists, sorted by size. Chunk Freelists Used Reserved C A A A A C A A C A Committed GC Threshold
  • 18. 21PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ  Freelists can get huge. – We have seen used:free ratios of 1:3 and worse – =>Metaspace is not really elastic.  Intra-chunk waste – At some point loader typically stops loading classes; remaining chunk space is wasted – Worse with many tiny loaders (reflection delegator classes, lambda anonymous classes)  Code bloat – Expensive to maintain. – Code base grew over time and has gotten overly complicated. Problems with the current implementation
  • 19. 22PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Huge freelists: Committed vs used space, after class unloading ~270M 
  • 20. 23PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ jcmd 27265 VM.metaspace 27265: … Waste (percentages refer to total committed size 373,48 MB): Committed unused: 280,00 KB ( <1%) Waste in chunks in use: 2,45 KB ( <1%) Free in chunks in use: 6,34 MB ( 2%) Overhead in chunks in use: 186,75 KB ( <1%) In free chunks: 269,56 MB ( 72%) Deallocated from chunks in use: 998,98 KB ( <1%) (1763 blocks) -total-: 277,33 MB ( 74%) Huge Freelists (jcmd VM.metaspace output)
  • 22. 26PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ  Uncommit chunks in freelists ● Makes Metaspace more elastic  Delay committing chunks until they are actually used ● Loader commits as needed (like a thread stack) ● Removes the penalty of handing out large chunks to class loaders ● Mitigates intrachunk waste problem (at least for large chunks) Basic idea
  • 23. 27PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ  (Linux): we decommit with mmap(MAP_NORESERVE) && mprotect(PROT_NONE) – Higher commit/uncommit fragmentation results in higher number of VMAs – Kernel keeps vma structures in list and rb tree – Too many of them may affect vma lookup – And we may hit process limits  So: keep an eye on commit granularity Concern: keep number of virtual memory areas low
  • 24. 28PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Commit granules Before: commit HWM Now: Granule size Adjustable, defaults to 64k
  • 25. 29PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ  Odd chunk geometry – Difficult to merge and split – High fragmentation – Complex code  Chunk headers are a problem Current chunk allocation scheme unsuited for uncommitting chunks
  • 26. 30PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ • Power 2 based buddy allocation scheme • Chunks sized from 1K … 4M in pow2 steps • Dead simple to split and merge. • Low external defragmentation -> Leads to larger free contiguous areas. • Standard algorithm widely known Pow 2 based buddy allocator for chunks
  • 27. 32PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Buddy allocator: Deallocation  Mark chunk as free  If buddy chunk is free and unsplit: remove from freelist and merge with chunk – Repeat until root chunk sized reached or until buddy is not free  Return result chunk to free list 64K 256K128K64K 64K 256K64K 64K 64K 512K 64K 256K64K 64K 64K
  • 28. 33PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Chunk headers needed to go page page page page ChunkHeaderPool page page page page Header Before Now
  • 29. 34PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Granules and chunks Chunks Granules 256K 64K 32 K 1 6 K 1 6 K 128K  A larger chunk can span multiple granules (1:n)  Multiple small chunks can cover a single granule (n:1) 1:n n:1
  • 30. 35PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Granules and chunks Chunks Granules 256K 64K 32 K 1 6 K 1 6 K 128K  Free chunks spanning 1+ granules can be uncommitted  A chunk spanning >1 granules can be committed on demand
  • 31. 36PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ • Got rid of humongous chunks :) • Got rid of occupancy map • Better deallocation management • Chunks can now often grow in-place • Saves overhead and reduces intrachunk waste • Code is cleaner and more maintainable; better separation of concerns and testability. What else changed
  • 32. 37PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Result: Committed vs used, Stock JDK14 ~270M 
  • 33. 38PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Result: Committed vs used, Patched JDK14
  • 34. 39PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Result: committed Metaspace, Stock vs Patched VM
  • 35. 40PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Result: RSS, Stock vs Patched VM
  • 36. 41PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ Bottomline: Results  Metaspace is now elastic. Freelist problem solved (almost).  Modest decrease in consumption even without mass class unloading ● Wildfly standalone after startup: 61m->54m, -7m, (11%) ● Eclipse CDT, hotspot project after C++ indexing: 138m->129m, -9m (12%) ● jruby helloworld.rb (invokedynamic, compile=FORCE): 41m->38m, -3m, (1.2%) ● → But, still more ideas and room for improvement.  Better overall code quality ● Better separation of concerns, better testability, more generic coding
  • 37. 42PUBLIC© 2019 SAP SE or an SAP affiliate company. All rights reserved. ǀ How do we go from here?  Patch is stable. Needs more tests and may need smaller fixes but it works.  Patch lives in jdk/sandbox repository, branch "stuefe-new-metaspace-branch“ – http://hg.openjdk.java.net/jdk/sandbox/  JEP exists in Draft state (“Elastic Metaspace”: https://openjdk.java.net/jeps/8221173 )  JDK15? – Very difficult to bring such a large patch upstream  A good candidate for backporting! – Would make a lot of sense in 11/8 – Large patch but Metaspace is quite isolated. Should not be too much of a hassle.
  • 39. Q/A