Under the Hood of .NET Memory Management

pdf
Số trang Under the Hood of .NET Memory Management 225 Cỡ tệp Under the Hood of .NET Memory Management 7 MB Lượt tải Under the Hood of .NET Memory Management 0 Lượt đọc Under the Hood of .NET Memory Management 0
Đánh giá Under the Hood of .NET Memory Management
4.9 ( 21 lượt)
Nhấn vào bên dưới để tải tài liệu
Đang xem trước 10 trên tổng 225 trang, để tải xuống xem đầy đủ hãy nhấn vào bên trên
Chủ đề liên quan

Nội dung

.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 system, or transmitted, in any form, or by any means (electronic, mechanical, photocopying, recording or otherwise) 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............................................................................................................. 141 Excessive Writes and Hitting the Write Barrier............................................................... 142 Fragmentation....................................................................................................................... 143 Long-Lived Objects............................................................................................................... 143 Conclusion.............................................................................................................................144 Chapter 5: Application-Specific Problems.................................. 146 Introduction...........................................................................................................................146 IIS and ASP.NET....................................................................................................................146 Caching.............................................................................................................................147 Debug................................................................................................................................ 152 StringBuilder.................................................................................................................... 154 ADO.NET................................................................................................................................155 LINQ.................................................................................................................................. 156 Windows Presentation Foundation (WPF).......................................................................160 Event handlers................................................................................................................ 160 Weak event pattern......................................................................................................... 163 Command bindings........................................................................................................168 Data binding.....................................................................................................................169 Windows Communication Framework.............................................................................170 Benefits.............................................................................................................................. 171 Drawbacks........................................................................................................................ 171 Disposable......................................................................................................................... 173 Configuration................................................................................................................... 175 Conclusion............................................................................................................................. 177 Section 3: Deeper .NET.....................................................178 Chapter 6: A Few More Advanced Topics.................................... 179 Introduction...........................................................................................................................179 32-Bit vs. 64-Bit......................................................................................................................180 Survey of Garbage Collection Flavors................................................................................ 182 Garbage Collection Notification.........................................................................................186 Weak References....................................................................................................................190 Marshaling.............................................................................................................................. 192 Conclusion............................................................................................................................. 195 Chapter 7: The Windows Memory Model.................................... 197 .NET/OS Interaction............................................................................................................ 197 Virtual and Physical Memory..............................................................................................198 Pages................................................................................................................................. 199 The process address space............................................................................................200 Memory Manager..................................................................................................................201 Using the memory manager......................................................................................... 202 Keeping track.................................................................................................................. 202 Page Frame Database........................................................................................................... 204 The Page Table.......................................................................................................................205 Virtual Addresses and the Page Table............................................................................... 206 Page Table Entry................................................................................................................... 208 Page Faults............................................................................................................................. 208 Locking Memory.................................................................................................................. 209 Putting It All Together..........................................................................................................210 Memory Caching....................................................................................................................211 The Long and Winding Road.............................................................................................. 212 Summary................................................................................................................................. 212 About the authors Chris Farrell Chris has over 20 years of development experience, and has spent the last seven as a .NET consultant and trainer. For the last three years, his focus has shifted to application performance assurance, and the use of tools to identify performance problems in complex .NET applications. Working with many of the world's largest corporations, he has helped development teams find and fix performance, stability, and scalability problems with an emphasis on training developers to find problems independently in the future. In 2009, after working at Compuware as a consultant for two years, Chris joined the independent consultancy, CodeAssure UK (www.codeassure.co.uk) as their lead performance consultant. Chris has also branched out into mobile app development and consultancy with Xivuh Ltd (www.xivuh.com) focusing on Apple's iOS on iPhone and iPad. When not analyzing underperforming websites or writing iPhone apps, Chris loves to spend time with his wife and young son swimming, bike riding, and playing tennis. His dream is to encourage his son to play tennis to a standard good enough to reach a Wimbledon final, although a semi would be fine. Acknowledgements I have to start by acknowledging Microsoft's masterpiece, .NET, because I believe it changed for the better how apps were written. Their development of the framework has consistently improved and enhanced the development experience ever since. viii I also want to thank the countless developers, load test teams and system admins I have worked with over the last few years. Working under enormous pressure, we always got the job done, through their dedication and hard work. I learn something new from every engagement and, hopefully, pass on a lot of information in the process. I guess it's from these engagements that I have learnt how software is actually developed and tested out in the real world. That has helped me understand the more common pitfalls and misunderstandings. I must also acknowledge Red Gate, who are genuinely committed to educating developers in best practice and performance optimization. They have always allowed me to write freely, and have provided fantastic support and encouragement throughout this project. I want to also thank my editor, Chris Massey. He has always been encouraging, motivating and very knowledgeable about .NET and memory management. Finally, I want to thank my wife Gail and young son Daniel for their love, support and patience throughout the research and writing of this book. Chris Farrell Nick Harrison Nick is a software architect who has worked with the .NET framework since its original release. His interests are varied and span much of the breadth of the framework itself. On most days, you can find him trying to find a balance between his technical curiosity and his family/personal life. ix Acknowledgements I would like to thank my family for their patience as I spent many hours researching some arcane nuances of memory management, occasionally finding contradictions to long-held beliefs, but always learning something new. I know that it was not always easy for them. Nick Harrison About the Technical Reviewer Paul Hennessey is a freelance software developer based in Bath, UK. He is an MCPD, with a B.Sc. (1st Class Hons.) and M.Sc. in Computer Science. He has a particular interest in the use of agile technologies and a domain-driven approach to web application development. He spent most of his twenties in a doomed but highly enjoyable bid for fame and fortune in the music business. Sadly, a lot of memories from this period are a little blurred. When he came to his senses he moved into software, and spent nine years with the leading software engineering company, Praxis, working on embedded safety-critical systems. Sadly, very few memories from this period are blurred. He now spends his time building web applications for clients including Lloyds Banking Group, South Gloucestershire Council, and the NHS. He is also an active member of the local .NET development community. x
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.