Under the Hood of .NET Memory Management Chris Farrell and Nick Harrison
Total Page:16
File Type:pdf, Size:1020Kb
.NET Handbooks Under the Hood of .NET Memory Management Chris Farrell and Nick Harrison ISBN: 978-1-906434-74-8 Under the Hood of .NET Memory Management By Chris Farrell and Nick Harrison First published by Simple Talk Publishing November 2011 Copyright November 2011 ISBN 978-1-906434-74-8 The right of Chris Farrell and Nick Harrison to be identified as the authors of this work has been asserted by them in accordance with the Copyright, Designs and Patents Act 1988. All rights reserved. No part of this publication may be reproduced, stored or introduced into a retrieval sys- tem, or transmitted, in any form, or by any means (electronic, mechanical, photocopying, recording or oth- erwise) without the prior written consent of the publisher. Any person who does any unauthorized act in relation to this publication may be liable to criminal prosecution and civil claims for damages. This book is sold subject to the condition that it shall not, by way of trade or otherwise, be lent, re-sold, hired out, or otherwise circulated without the publisher's prior consent in any form other than that in which it is published and without a similar condition including this condition being imposed on the subsequent publisher. Technical Review by Paul Hennessey Cover Image by Andy Martin Edited by Chris Massey Typeset & Designed by Matthew Tye & Gower Associates. Table of Contents Section 1: Introduction to .NET Memory Management ..13 Chapter 1: Prelude ......................................................................... 14 Overview .................................................................................................................................. 15 Stack .........................................................................................................................................16 Heap .........................................................................................................................................18 More on value and reference types .............................................................................. 20 Passing parameters ...........................................................................................................23 Boxing and unboxing ...................................................................................................... 26 More on the Heap ..................................................................................................................27 Garbage collection ........................................................................................................... 28 Static Objects ..........................................................................................................................32 Static methods and fields ................................................................................................ 33 Thread Statics ...................................................................................................................34 Summary .................................................................................................................................. 35 Chapter 2: The Simple Heap Model ..............................................36 Managed Heaps ......................................................................................................................36 How big is an object? .......................................................................................................36 Small Object Heap...........................................................................................................40 Optimizing garbage collection .......................................................................................43 Generational garbage collection ................................................................................... 44 Finalization ....................................................................................................................... 50 Large Object Heap ...........................................................................................................55 Summary ..................................................................................................................................58 Chapter 3: A Little More Detail ....................................................59 What I Didn't Tell You Earlier ..............................................................................................59 The card table...................................................................................................................60 A Bit About Segments ............................................................................................................62 Garbage Collection Performance ........................................................................................ 64 Workstation GC mode ................................................................................................... 64 Server GC mode ............................................................................................................... 67 Configuring the GC........................................................................................................ 68 Runtime GC Latency Control ............................................................................................. 69 GC Notifications .................................................................................................................... 70 Weak References .....................................................................................................................73 Under the hood................................................................................................................ 74 More on the LOH ...................................................................................................................75 Object Pinning and GC Handles .........................................................................................76 GC Handles .......................................................................................................................77 Object pinning ..................................................................................................................77 Problems with object pinning ....................................................................................... 78 Summary ..................................................................................................................................78 Section 2: Troubleshooting ..............................................80 What's Coming Next ..................................................................... 81 Language ..................................................................................................................................81 Best practices ..........................................................................................................................82 Symptom flowcharts ............................................................................................................. 84 Chapter 4: Common Memory Problems .......................................86 Types ........................................................................................................................................87 Value types ........................................................................................................................ 88 Reference types ................................................................................................................90 Memory Leaks.........................................................................................................................92 Disposing of unmanaged resources ..............................................................................93 Simple Value Types ............................................................................................................... 99 Overflow checking .........................................................................................................101 Strings ....................................................................................................................................103 Intern pool .......................................................................................................................105 Concatenation ................................................................................................................107 Structs ....................................................................................................................................109 Classes .....................................................................................................................................117 Size of an Object ....................................................................................................................117 Delegates ................................................................................................................................124 Closures............................................................................................................................128 Effects of Yield ......................................................................................................................129 Arrays and Collections ......................................................................................................... 136 Excessive References ...........................................................................................................