Www.red-gate.com/memoryprofiler 5 Misconceptions about.NET Memory Management Wednesday June 1 st 2011 Clive Tong and Andrew Hunter.

Slides:



Advertisements
Similar presentations
An Implementation of Mostly- Copying GC on Ruby VM Tomoharu Ugawa The University of Electro-Communications, Japan.
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Pay-to-use strong atomicity on conventional hardware Martín Abadi, Tim Harris, Mojtaba Mehrara Microsoft Research.
A Block-structured Heap Simplifies Parallel GC Simon Marlow (Microsoft Research) Roshan James (U. Indiana) Tim Harris (Microsoft Research) Simon Peyton.
10 dingen die je niet wist over C#. ALM ALM Ranger Microsoft Auteur Getrouwd Lezen C#
Automatic Memory Management Noam Rinetzky Schreiber 123A /seminar/seminar1415a.html.
ASP.NET Best Practices Dawit Wubshet Park University.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
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++)
By Jacob SeligmannSteffen Grarup Presented By Leon Gendler Incremental Mature Garbage Collection Using the Train Algorithm.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 21: Garbage Collection.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
The Type System1. 2.NET Type System The type system is the part of the CLR that defines all the types that programmers can use, and allows developers.
G Robert Grimm New York University Cool Pet Tricks with… …Virtual Memory.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Generational Stack Collection And Profile driven Pretenuring Perry Cheng Robert Harper Peter Lee Presented By Moti Alperovitch
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Age-Oriented Concurrent Garbage Collection Harel Paz, Erez Petrank – Technion, Israel Steve Blackburn – ANU, Australia April 05 Compiler Construction Scotland.
Garbage collection (& Midterm Topics) David Walker COS 320.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
CLR: Garbage Collection Inside Out
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Ulterior Reference Counting: Fast Garbage Collection without a Long Wait Author: Stephen M Blackburn Kathryn S McKinley Presenter: Jun Tao.
File I/O Applied Component-Based Software Engineering File I/O CSE 668 / ECE 668 Prof. Roger Crawfis.
Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing.
A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management.
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.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
.NET 2006 Using the.NET Profiler API to Collect Object Instances for Constraint Evaluation Presented by: Dave Arnold 4 th International Conference on.NET.
Copyright (c) 2004 Borys Bradel Myths and Realities: The Performance Impact of Garbage Collection Paper: Stephen M. Blackburn, Perry Cheng, and Kathryn.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 17: Garbage Collection.
1 File Systems: Consistency Issues. 2 File Systems: Consistency Issues File systems maintains many data structures  Free list/bit vector  Directories.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 22: Unconventional.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
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.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Tips & Tricks: Writing Performant Managed Code Rico Mariani FUNL04 Performance Architect Microsoft Corporation.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 12: Automating Memory Management
Beyond Application Profiling to System Aware Analysis Elena Laskavaia, QNX Bill Graham, QNX.
.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.
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
CSC 533: Programming Languages Spring 2016
Topic: Java Garbage Collection
CSC 533: Programming Languages Spring 2015
Module 9: Memory and Resource Management
Java 9: The Quest for Very Large Heaps
CS 153: Concepts of Compiler Design November 28 Class Meeting
Runtime Analysis of Hotspot Java Virtual Machine
Advanced Programming Behnam Hatami Fall 2017.
Smart Pointers.
Closure Representations in Higher-Order Programming Languages
Programming and Debugging
Multi-modules programming
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
X86 Assembly Review.
Low-Level Thread Dispatching on the x86
Problems with Locks Andrew Whitaker CSE451.
Automating Memory Management
CSC 533: Programming Languages Spring 2019
CMPE 152: Compiler Design May 2 Class Meeting
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

5 Misconceptions about.NET Memory Management Wednesday June 1 st 2011 Clive Tong and Andrew Hunter

Developer,.NET Reflector Developer, ANTS Memory Profiler Your presenters

The webinar Clive: 5 misconceptions about.NET memory management (30 mins) Andrew: Q&A session for any questions to do with.NET memory $50 Amazon voucher for the best answer to Clives question. Please type questions into your GoToWebinar chat box

Some Misconceptions About Memory Management in.NET Clive Tong Red Gate Software

Five misconceptions 1 A garbage collector collects garbage 2 Doing lots of gen0 collections is bad 3 Performance counters are great for understanding what is happening 4.NET doesnt leak memory 5 All objects are treated the same

1) The Garbage Collectors Collect Garbage The focus is really on the non-garbage Most (young) objects die young Designed to collect dead items without processing them individually Note this is for generation 0

A simple mutator var collect = new List (); while(true) { collect.Add(new A()); new A(); new A(); }

Allocation in generation 0 Generation 0 After one iteration of the while loop As we go into the fourth iteration

No space, so copy Promote gen0 gen1

Fix-up gen0 gen1

And recycle gen0 gen1

Observations Trick is that objects are allowed to move Need to get to a safe point when pointers can be fixed Additionally this allows quick allocation

Fast allocation 0:000> u 002c2020 mov eax,dword ptr [ecx+4] mov edx,dword ptr fs:[0E40h] add eax,dword ptr [edx+48h] cmp eax,dword ptr [edx+4Ch] ja 002c203b mov dword ptr [edx+48h],eax sub eax,dword ptr [ecx+4] mov dword ptr [eax],ecx ret 002c203b: jmp clr!JIT_NewFast (5cbded01)

Solution Avoid locking too frequently by having thread specific allocation buffers Thread 1 allocation buffer Thread 2 allocation buffer

And there is one other trick Use generations to focus attention – 0,1,2 Need to track object references 0:000> u 5cbc2fb0 clr!JIT_WriteBarrierEAX: mov dword ptr [edx],eax cmp eax,271100Ch jb clr!JIT_WriteBarrierEAX+0x17 (5cbc2fc7) shr edx,0Ah nop cmp byte ptr [edx+0BD63E0h],0FFh jne clr!JIT_WriteBarrierEAX+0x1a (5cbc2fca) ret clr!JIT_WriteBarrierEAX+0x18: nop mov byte ptr [edx+0BD63E0h],0FFh ret

2) Lots of gen0 collections is bad We have covered this already Time proportional to live data Though with (fixed) overheads Worse case double allocation

3) Performance counters are accurate Quick demonstration

Periodic measurements Its important to remember that these are updated when a collection happens No collection means the counter is stuck The average value can be misleading

class Program { static void Main(string[] args) { var accumulator = new List (); while (true) { DateTime start = DateTime.Now; while ((DateTime.Now - start).TotalSeconds < 15) { accumulator.Add(new Program()); Thread.Sleep(1); } Console.WriteLine(accumulator.Count); } } }

4).NET doesnt leak Its a question of definition Old definition -Forgot to de-allocate after use -Lost the final pointer to some memory -Forgot your responsibilities

New definition Premature release was often fatal Those days are gone (*) The runtime is ultra-cautious Its difficult to have an effective cost model (*) except when you aggressively dispose

What makes things live longer? Runtime User Library Compiler

Runtime Type of build Having a debugger attached Heuristics choosing when to collect higher generations Finalizers

User Event handlers Assigning temporary values to fields Value only needed on some code paths

Library Caches without a lifetime policy Data binding

Compiler Closures

Closures class Program { private static Func s_LongLived; static void Main(string[] args) { var x = 20; var y = new int[20200]; Func getSum = () => x + y.Length; Func getFirst = () => x; s_LongLived = getFirst; }

5) All objects are created equal Copying takes a while Need a better way to handle large objects Resort to standard mark-and-sweep No compact at the moment

No copying during collection Before After

Some observations No movement, so no fix-ups Potential parallelism But potential fragmentation Only do when gen2 is collected Temporary large objects dont fit model All blocks are touched

The problem of fragmentation Before collection After collection Next allocation Allocate block of this size:

Conclusion Theres a lot going on inside your process heap The only way to really understand what is going on is to use tools to visualize things

Q&A Session Please type your questions into the GoToWebinar box We will read out your questions for Andrew to answer

Thank you! A recording of this webinar will be ed to you tomorrow Do get in touch!