1 Thread Synchronization (Un)Fairness in the.NET Common Language Runtime If you are building an application that absolutely requires fair thread synchronization,

Slides:



Advertisements
Similar presentations
Paging: Design Issues. Readings r Silbershatz et al: ,
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Part IV: Memory Management
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.
Programming Types of Testing.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
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.
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
CPSC 388 – Compiler Design and Construction
CS 536 Spring Automatic Memory Management Lecture 24.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
1 Storage Allocation Operating System Hebrew University Spring 2007.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Chapter 3.7 Memory and I/O Systems. 2 Memory Management Only applies to languages with explicit memory management (C or C++) Memory problems are one of.
Computer Organization and Architecture
Introducing the Common Language Runtime for.NET. The Common Language Runtime The Common Language Runtime (CLR) The Common Language Runtime (CLR) –Execution.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Process Description and Control A process is sometimes called a task, it is a program in execution.
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
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
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.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
tom perkins1 XML Web Services -.NET FRAMEWORK – Part 1 CHAPTER 1.1 – 1.3.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Threads G.Anuradha (Reference : William Stallings)
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
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.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
Memory Management -Memory allocation -Garbage collection.
Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
® 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.
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
.NET Memory Primer Martin Kulov. "Out of CPU, memory and disk, memory is typically the most important for overall system performance." Mark Russinovich.
Memory Management CSCI 2720 Spring What is memory management? “the prudent utilization of this scarce resource (memory), whether by conservation,
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Chapter 4 – Thread Concepts
Garbage Collection What is garbage and how can we deal with it?
Chapter 4 – Thread Concepts
Chapter 9 – Real Memory Organization and Management
Automatic Memory Management
Garbage collection; lightstick orientation
Main Memory Management
Storage.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
CSCE 313 – Introduction to UNIx process
Chapter 12 Memory Management
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
CS703 - Advanced Operating Systems
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

1 Thread Synchronization (Un)Fairness in the.NET Common Language Runtime If you are building an application that absolutely requires fair thread synchronization, you should not use the.NET Framework at all.“ Jeffrey Richter, In his article "Thread Synchronization Fairness in the.NET CLR”, published 6 February, 2003)

2 What?... Why? The CLR manages memory via garbage collection. When the CLR wants to start a garbage collection, … the CLR will suspend the threads executing managed code. When Windows suspends a thread, it stops the thread from waiting for any thread synchronization object. Later, when the thread is resumed, all the suspended threads race back to wait on the object that it was waiting on before it got suspended. This means that threads are not guaranteed to gain ownership of an object on a first-in-first-out basis. Jeffrey Richter, In his article "Thread Synchronization Fairness in the.NET CLR”, published 6 February, 2003)

3 So what is this presentation about? Thread Synchronization Garbage Collection in.NET managed application. How these seemingly unrelated areas conspire to cause unfair thread synchronization in the.NET CLR.

4 First, Thread Synchronization: Thread Synch support exposed in the underlying OS (and used by the.NET CLR) OS Kernel Support for Thread Synch Thread Synch in the CLR

5 Managed execution in the common language runtime (CLR) Figure 2-1, Introducing Microsoft.NET, Second Edition by David S. Platt

6 Thread Synch Support in Win2k User Mode User Mode – No transition to Kernel Mode* Interlocked Functions – Set simple 32 bit values atomically. Critical Sections – Data structure owned by one thread at a time to synchronize access to common code. Can be used to manipulate complex data structures in a thread safe manner. * When entering a critical section, the thread stays in user mode only as long as there is no contention. If the critical section is owned by another thread, the thread will transition to kernel mode and enter a wait state.

7 Thread Synch Support in Win2k Kernel Mode Provided Primarily through the Win32 subsystem Wait functions (i.e. WaitForSingleObject) A thread calls a Wait function to place itself in a wait state until a kernel object is signaled.

8 Waiting on a dispatcher object Figure 3-19, Inside Microsoft Windows 2000, Third Edition by David A. Solomon and Mark E. Russinovich

9 Definitions of the Signaled State Table 3-9: Inside Microsoft Windows 2000, Third Edition by David A. Solomon and Mark E. Russinovich

10 Running in the CLR Loading and initializing the CLR Figure 1-3, Applied Microsoft.NET Framework Programming by Jeffrey Richter

11 Threads in a Managed Process Each application thread in a managed application has 1:1 correspondence with an OS thread. The CLR will create additional threads for its own purposes: Finalizer, Debug, Threadpool, Asynch Ops (Timers, Delegates) All thread creation, dispatch and execution is done in the OS. The CLR itself provides none of this functionality.

12 Thread Synch Classes in.NET Adaptation of Table in Robert Burns, Multithreaded Programming with Visual Basic.NET, MSDN Online Library, February 2002

13 The other piece of the puzzle … Automatic Memory Management… a.k.a. Garbage Collection What is it and why do we use it?

14 Traditional Manual Memory Management (e.g. C-Runtime) Memory Allocation (malloc): User code requests a certain number of bytes. CRT walks linked list to find large enough memory block then updates list. Memory Deallocation (free): CRT updates linked list to free up the memory block.

15 Automatic Memory Management (e.g. Garbage Collection) CLR reserves contiguous block of memory when process initialized, the managed heap. Pointer maintained to next free location. Memory Allocation (newobj IL instruction): CLR determines memory required based on object metadata. If enough memory available, the CLR returns a pointer to next free location, and updates the pointer to the next free location. If not enough memory in the heap, a garbage collection (details later). Memory Deallocation: Memory is never explicitly deallocated, and is not freed until the next garbage collection is done.

16 Memory Management Comparison Manual Memory MgtAutomatic Memory Mgt AdvMore power to programmerSimple and fast allocation Predictable time delayGood Object Locality Deterministic Finalization DisadvMore power to programmerNot predictable – usually short / sometimes (unpredictably) long Introduces global dependenciesNon-deterministic finalization Most allocations slowerUser has less power Less object locality Memory leaks Early deallocation – memory corruption Fragile Code (static allocations) Application specific GC awkward & buggy

17 Garbage Collection in the.NET CLR The.NET CLR uses a Mark-Compact type garbage collector that utilizes generations and a separate Large Object Heap …

18 Garbage Collection = Detection + Reclamation Mark-Compact type garbage collection Mark – garbage detection GC creates a list of live objects by starting at a set of roots and walking all referenced objects, marking them as reachable. Anything not marked is reclaimable garbage. Other types of marking: Reference counting – Number of object references maintained. Copying – copy live objects as they are found to another space.

19 Garbage Collection = Detection + Reclamation Mark-Compact type garbage collection Compact – garbage reclamation GC walks linearly through the heap looking for contiguous garbage (now free) blocks that it can move live objects down to. In this way live objects are moved to the front of the heap and free space is ‘squeezed’ to the end. Pointers to moved objects are updated. Other types of reclamation: Copying: copy live objects as they are found to another space. Sweep: Garbage marked as free, but memory left fragmented.

20 Mark-Compact GC Mark-Compact Type Garbage Collection AdvProvides contiguous section of memory for easy future memory allocation. Object ordering maintained / locality somewhat maintained Circular references not a problem (e.g. garbage objects that reference each other – keeping themselves alive) DisadvMemory cannot be accessed during garbage collection. Several Passes over data required – marking, compute new locations, update pointers, actual object move. Time consuming if a lot of live data left. Generations used to help this situation …

21 Generations Most objects live a very short time. “80-98% of new objects die within a few million CPU instructions.” Paul R. Wilson, Uniprocessor Garbage Collection Techniques, 1992 International Workshop on Memory Management, St. Malo, France, September A small percent live much longer. Using generations capitilizes on these statistics: Each time an object survives a GC, it is graduated to a higher generation (.NET currently supports generations 0,1, and 2). The garbage collector will normally do a generation 0 collection. This will lessen the amount of long lived live objects that must be marked and copied during a GC.

22 Large Object Heap Any objects that are 85,000 bytes or more are allocated on a separate heap – the Large Object Heap. The Large Object Heap is never compacted. Done to alleviate the need to copy large objects during the compact phase of the garbage reclamation.

23 How does this all tie together? Lets reexamine one of the disadvantages listed for the Mark-Compact type GC: Memory cannot be accessed during garbage collection. In the case we are examining, the.NET CLR on a uniprocessor system, this means that all threads that are running managed code in the process must be suspended while the GC moves memory and fixes up pointers.

24 The problem restated A request is made to instantiate a new object, if there is not enough room in generation 0 for this object, then a garbage collection is run. During a garbage collection, all threads that are running managed code are suspended. “When Windows suspends a thread, it stops the thread from waiting for any thread synchronization object. Later, when the thread is resumed, all the suspended threads race back to wait on the object that it was waiting on before it got suspended. This means that threads are not guaranteed to gain ownership of an object on a first-in-first-out basis. Since a garbage collection can start at any time (and cannot be prevented), the architecture of the CLR just doesn't support fair thread synchronization — period. “ Jeffrey Richter, In his article "Thread Synchronization Fairness in the.NET CLR”, published 6 February, 2003)

25 References General: Jeffrey Richter, Thread Synchronization Fairness in the.NET CLR. published 6 February, Francesco Balena, Programming Microsoft Visual Basic.NET, Microsoft Press © Aaron Barth, Jackie Richards, Production Debugging for.NET Framework Applications, Microsoft Patterns and Practices, MSDN Online Library, November 2002, ( ) Robert Burns, Multithreaded Programming with Visual Basic.NET, MSDN Online Library, February 2002 ( us/dv_vstechart/html/vbtchasyncprocvb.asp ) us/dv_vstechart/html/vbtchasyncprocvb.asp Jeffrey Richter, Applied Microsoft.NET Framework Programming. Microsoft Press © Jeffrey Richter, Thread Synchronization Fairness in the.NET CLR. published 6 February, Windows David A. Solomon and Mark E. Russinovich, Inside Microsoft Windows 2000, Third Edition. Microsoft Press © Jeffrey Richter, Programming Applications for Microsoft Windows, Fourth Edition. Microsoft Press © Garbage Collection Paul R. Wilson, Uniprocessor Garbage Collection Techniques, 1992 International Workshop on Memory Management, St. Malo, France, September 1992

26 Questions? As clear as mud?