CLR: Garbage Collection Inside Out

Slides:



Advertisements
Similar presentations
Advanced Troubleshooting with Debug Diagnostics on IIS 6
Advertisements

An Implementation of Mostly- Copying GC on Ruby VM Tomoharu Ugawa The University of Electro-Communications, Japan.
Memory.
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Wenjin Xu. BasicsAllocationCollectionFinalization GC Mode Sample.
A Block-structured Heap Simplifies Parallel GC Simon Marlow (Microsoft Research) Roshan James (U. Indiana) Tim Harris (Microsoft Research) Simon Peyton.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
DEV300: A Tiny CPU and OS in C# Scott Hanselman Technology Evangelist/Architect Corillian Corporation
Memory Management: Overlays and Virtual Memory
The Linux Kernel: Memory Management
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Memory Management Tom Roeder CS fa. Motivation Recall unmanaged code eg C: { double* A = malloc(sizeof(double)*M*N); for(int i = 0; i < M*N; i++)
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Spring 2014Jim Hogg - UW - CSE P501W-1 CSE P501 – Compiler Construction Conventional Heap Storage Garbage Collection.
NUMA Tuning for Java Server Applications Mustafa M. Tikir.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
This presentation: Sasha GoldshteinCTO, Sela Group Garbage Collection Performance Tips.
Chapter 10 Storage management
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
JETT 2003 Java.compareTo(C++). JAVA Java Platform consists of 4 parts: –Java Language –Java API –Java class format –Java Virtual Machine.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Memory Management 2010.
Compilation 2007 Garbage Collection Michael I. Schwartzbach BRICS, University of Aarhus.
CS 333 Introduction to Operating Systems Class 9 - Memory Management
Computer Organization and Architecture
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
Memory-Mapped Files & Unified VM System Vivek Pai.
Virtual Memory Chantha Thoeun. Overview  Purpose:  Use the hard disk as an extension of RAM.  Increase the available address space of a process. 
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
Page 19/17/2015 CSE 30341: Operating Systems Principles Optimal Algorithm  Replace page that will not be used for longest period of time  Used for measuring.
CS333 Intro to Operating Systems Jonathan Walpole.
File I/O Applied Component-Based Software Engineering File I/O CSE 668 / ECE 668 Prof. Roger Crawfis.
GC Algorithm inside.NET Luo Bingqiao 5/22/2009. Agenda 1. 经典基本垃圾回收算法 2.CLR 中垃圾回收算法介绍 3.SSCLI 中 Garbage Collection 源码分析.
CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging.
CS 390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Distributed Computing Fundamentals.
Ideas to Improve SharePoint Usage 4. What are these 4 Ideas? 1. 7 Steps to check SharePoint Health 2. Avoid common Deployment Mistakes 3. Analyze SharePoint.
8.4 paging Paging is a memory-management scheme that permits the physical address space of a process to be non-contiguous. The basic method for implementation.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
tom perkins1 XML Web Services -.NET FRAMEWORK – Part 1 CHAPTER 1.1 – 1.3.
Writing faster managed code Claudio Caldato Program Manager CLR Performance Team.
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
1 Memory Management Basics. 2 Program P Basic Memory Management Concepts Address spaces Physical address space — The address space supported by the hardware.
Operating Systems Lecture 14 Segments Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software Engineering.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
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.
Processes and Virtual Memory
Memory Management: Overlays and Virtual Memory. Agenda Overview of Virtual Memory –Review material based on Computer Architecture and OS concepts Credits.
Tips & Tricks: Writing Performant Managed Code Rico Mariani FUNL04 Performance Architect Microsoft Corporation.
“WALK IN” SLIDE. August Taming the CLR: How to Write Really Fast Managed Code Rico Mariani Architect Developer Division Performance Team Microsoft.
Virtual Memory Pranav Shah CS147 - Sin Min Lee. Concept of Virtual Memory Purpose of Virtual Memory - to use hard disk as an extension of RAM. Personal.
DEV414 Black-belt ASP.NET Tips And Tricks For Your ASP.NET Applications Rob Howard Program Manager Web Platform and Tools Team.
® 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.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Lecture 10 Page 1 CS 111 Online Memory Management CS 111 On-Line MS Program Operating Systems Peter Reiher.
.NET Garbage Collection Performance Tips Sasha Goldshtein | SELA Group.
.NET Memory Primer Martin Kulov. "Out of CPU, memory and disk, memory is typically the most important for overall system performance." Mark Russinovich.
Processes and threads.
Java 9: The Quest for Very Large Heaps
Swapping Segmented paging allows us to have non-contiguous allocations
Lecture 10: Buffer Manager and File Organization
Patrick Dussud Technical Fellow Developer Division
CS399 New Beginnings Jonathan Walpole.
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
CS703 - Advanced Operating Systems
CS703 – Advanced Operating Systems
COMP755 Advanced Operating Systems
Presentation transcript:

CLR: Garbage Collection Inside Out Maoni Stephens FUN421 Software Design Engineer Microsoft Corporation

Agenda Basics Generations Segments Allocation Collection Large Object Heap Different Flavors of GC Pinning Tools and Resources

Basics New Heap Begins with New Generation Accessible References Keep Objects Alive Compacts Referenced Objects Objects Promoted to Older Generation New Allocations Rebuild New Generation

Agenda Basics Generations Segments Allocation Collection Large Object Heap Different Flavors of GC Pinning Tools and Resources

Generational GC Three generations Most objects die in gen0 Gen1 holds in the in flight data Gen2 holds the long lived data Large object heap Gen0 and Gen1 – ephemeral generations

Agenda Basics Generations Segments Allocation Collection Large Object Heap Different Flavors of GC Pinning Tools and Resources

GC Heap Segments Unit of VirtualAlloc When CLR is loaded, initial segments are allocated Additional segments reserved as needed Committed/Decommitted as needed in the segment Deleted when not in use VM hoarding feature in next CLR 1.1 SP and CLR 2.0 – STARTUP_HOARD_GC_VM startup flag

Generations And Segments Start from the ephemeral segment Ephemeral segment becomes a gen2 segment Newly reserved segment becomes ephemeral Many gen2 segments, only one ephemeral

Before GC #0 Before GC #1 Before GC #2 Before GC #100 Before GC #500 Gen0 Before GC #0 Before GC #1 Gen1 Gen0 Before GC #2 Gen2 Gen1 Gen0 Before GC #100 Gen2 Gen1 Gen0 Before GC #500 Gen2 Gen1 Gen0

Agenda Basics Generations Segments Allocation Collection Large Object Heap Different Flavors of GC Pinning Tools and Resources

Allocation Cheap lock on UP; lock free on MP Moving a pointer forward Clearing the memory for new objects Register for finalization if applicable Objects allocated together are close together

Agenda Basics Generations Segments Allocation Collection Large Object Heap Different Flavors of GC Pinning Tools and Resources

Collection When Allocation. Not enough space in gen0. Induced GC. By calling System.GC.Collect(). System pressure.

Collection How Suspend managed threads GC Resume managed threads Two phases of GC Mark Compact

Roots Stack Handle table Statics Older generation(s) Finalizer queue

Agenda Basics Generations Segments Allocation Collection Large Object Heap Different Flavors of GC Pinning Tools and Resources

Large Object Heap For objects that are 85,000 bytes or larger Compacting large objects costs a lot So we only sweep (freelist) Many LOH segments Segments are handled similarly to small object heap segments Collection happens during gen2 GCs

Collection – Cost GC takes time – “% time in GC” counter If objects die in gen0 (survival rate is 0) it’s the ideal situation The longer the object lives before being dead, the worse (with exceptions) Gen0 and gen1 GCs should both be relatively cheap; gen2 GCs could cost a lot LOH – different cost model Temporary large objects could be bad Should reuse if possible

Agenda Basics Generations Segments Allocation Collection Large Object Heap Different Flavors of GC Pinning Tools and Resources

Concurrent GC Why do we have concurrent GC How it’s done For interactive applications How it’s done Trade some CPU and memory for shorter pause time Only for gen2 Done on a concurrent GC thread How do you get concurrent GC On by default Can be turned off via hosting or config

Server GC Why do we have server GC – for server apps that Have a fairly consistent number of requests Require high scalibility and high throughput How it’s done One thread for each CPU, running at highest priority One ephemeral segment per CPU How do you get server GC Only on via config or hosting Hosts like ASP.NET and SQLCLR use server GC In CLR 1.1 it’s in mscorsvr.dll; in CLR 2.0 it’s in mscorwks.dll

WKS GC SVR GC Where it runs On the user thread that triggered GC On the GC threads running at highest priority On a multi proc machine One small object heap + One LOH N small object heaps + N LOHs On a uni proc machine WKS GC + concurrent GC (if not turned off) WKS GC + concurrent GC OFF

Agenda Basics Generations Segments Allocation Collection Large Object Heap Different Flavors of GC Pinning Tools and Resources

Pinning Why do we need pinning How objects get pinned Interop with unmanaged code How objects get pinned Use of GCHandle of type GCHandleType.Pinned Allowed by language syntax, eg. fixed in c# Args get pinned by the interop frame Fragmentation problem

Fragmentation Problem Caused By Pinning Before GC X Gen1 Gen0 start P After GC X Gen2 P Gen1 start Gen0 start After N more GCs Gen2 P Gen1 P Gen0 start Gen1 start

Without demotion Gen1 start Gen0 start With demotion Gen1 start P Gen0 start With demotion Gen2 Gen1 start P Gen0 start

Without Segment Reuse Before GC After GC Seg0 Seg1 Seg2 Seg3 Eph Seg0 Gen2 Seg0 Seg1 Seg2 Seg3 Gen1 Eph P Gen0 Gen2 Seg0 Seg1 Seg2 Seg3 Old Eph P Gen1 New Eph

With Segment Reuse Before GC After GC Seg0 Seg1 Seg2 Seg3 Eph Seg0 Gen2 Seg0 Seg1 Seg2 Seg3 Gen1 Eph P Gen0 Gen2 Seg0 Seg1 Seg2 P Old Eph Old Seg3, New Eph Gen2 Gen1

What The User Code Can Do To Help Best patterns Pinning for a short time is cheap. Create pinned buffers at the beginning that will stay in regions that are very compacted Create pinned buffers that stay together instead of scattered around Techniques Allocate a pinned big buffer (byte array on the LOH), give out a chunk at a time Allocate a pool of small buffers and hand one out when needed

void M(byte[] b) { // if the buffer is already in gen2 it’s unlikely to move. if(GC.GetGeneration(b) == GC.MaxGeneration || // avoid copying if the buffer is too big b.Length > 8 * 1024) RealM(b); } // GetBuffer will allocate one if none is free. byte[] TempBuffer = Pool.GetBuffer(); RealM(TempBuffer); CopyBackToUserBuffer(TempBuffer, b); Pool.Release(TempBuffer);

Managed Caches Design patterns Cache tuning Don’t do it in the finalizer Use combination of weak and strong references – could convert strong to weak after certain period Cache tuning Hit rate Cost to add an item Cost to find an item Frequency of cleanup If not implemented carefully could easily cause more gen2 collections than needed

// sweep the cache for dead objects and compacts the list. static void sweep_WeakRef() { // don't bother if no new collection happened since // we swept last if (GC.CollectionCount(0) != gen0_count) gen0_count = GC.CollectionCount (0); else return; lock (list) { for (int i = 0; i < WeakRef_fill;) { if (list[i].Target == null) { // install the last element in the free slot. WeakRef_fill--; if (i != WeakRef_fill) { list[i] = list[WeakRef_fill]; list[WeakRef_fill] = null; } i++;

Why We Don’t Allow Many Customized Settings Only allow very few settings specified via hosting API or config Advantages for GC to do the tuning for you GC has intimate knowledge of memory when applications run GC is tested on many platforms + many configurations

Looking Ahead Challenges on 64-bit More common as people start using 64-bit machines VM is practically unlimited so physical memory is the limit Servers like the SQL Server tend to not allow paging Gen2 could take a long time

Tools And Resources http://blogs.msdn.com/maoni/ CLRProfiler .NET CLR Memory performance counters The SoS Debugger Extension for windbg/cdb vadump Task Manager

© 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.