C# Memory Tricks: Learn How To Master The Garbage Collector
4.5 (57 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
1,088 students enrolled
Wishlisted Wishlist

Please confirm that you want to add C# Memory Tricks: Learn How To Master The Garbage Collector to your Wishlist.

Add to Wishlist

C# Memory Tricks: Learn How To Master The Garbage Collector

This course teaches you advanced C# memory management tricks that every professional .NET developer must know.
4.5 (57 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
1,088 students enrolled
Created by Mark Farragher
Last updated 3/2017
English
Price: $90
30-Day Money-Back Guarantee
Includes:
  • 3.5 hours on-demand video
  • 2 Articles
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
What Will I Learn?
  • Learn how the Garbage Collector works
  • Master .NET memory optimization
  • Discover the truth about finalizers
  • Learn how to measure the memory footprint of your code
  • The unexpected memory footprint of List resizing
  • Structs versus classes - which one is better?
  • What assumptions does the GC make about object size and lifetime?
  • Manual deallocation with the Dispose pattern
  • ... and much more!
View Curriculum
Requirements
  • You need a Windows, Mac or Linux laptop with Microsoft Visual Studio, Xamarin Studio or Monodevelop
  • You should be a beginner, intermediate, or advanced C# developer
  • That's it!
Description

Last update: March 14, 2017 - added a lecture on how to modify strings directly using pointers.

BEST COURSE CHOICE for beginners and intermediate C# developers who want to learn how to master the garbage collector and take their programming skills to the next level.

Modern computers have loads of memory. But it's very easy to burn through it all in seconds if your code is not efficient about allocating and using memory.

Did you know that one simple mistake can make your code allocate 1600 times more memory than absolutely necessary?

Don't be 'that developer' who keeps crashing the development server with an OutOfMemory exception!

And you certainly don't want to be responsible for inflating the hardware budget. Can you imagine having to explain to your team that 512 GB of memory is not enough to run your code on the production server?

Let me help you.

It doesn't have to be like this. If you have a good understanding of the garbage collection process and follow a few simple best practices, you can dramatically reduce the memory footprint of your code.

Sound good?

In the last 10 years I have learned the secrets of garbage collection in .NET, and in this course I am going to share them all with you.

In a series of short lectures I will take a detailed look at the garbage collection process. I will show you all of the memory allocation problems you can expect when writing C# code, like unexpected boxing, string duplication, collection resizing, and more. I'll teach you quick and easy strategies to resolve these problems.

By the end of this course you will be able to master the garbage collector.

Why should you take this course?

You should take this course if you are a beginner or intermediate C# developer and want to take your skills to the next level. Garbage collection and memory management might sound complicated, but all of my lectures are very easy to follow and I explain all topics with clear code and many instructive diagrams. You'll have no trouble following along.

Or maybe you're working on a critical section of code in a C# project, and need to make sure your memory usage is as efficient as possible? The tips and tricks in this course will help you immensely.

Or maybe you're preparing for a C# related job interview? This course will give you an excellent foundation to answer any questions they might throw at you.

30 day money-back guarantee

This course comes with an unconditional, Udemy backed, 30-day money-back guarantee. If you are dissatisfied with the course for any reason, simply request a refund and get your full purchase amount back, no questions asked.

Act now

This course today contains over 3 hours of video content. Enroll now to benefit from the current price and get free lifelong access to all future materials.

Who is the target audience?
  • Beginner, intermediate, and advanced C# programmers who want to learn how to master the garbage collector.
  • Developers who are about to take a job interview and need to prepare for questions about memory allocation in .NET
  • Professionals who are writing a section of mission-critical code in a large C# project
Students Who Viewed This Course Also Viewed
Curriculum For This Course
Expand All 27 Lectures Collapse All 27 Lectures 03:27:24
+
Introduction
5 Lectures 17:00

In this lecture I explain how this course is organised and I describe each of the upcoming sections in detail.

Preview 04:12

In this lecture I will tell a bit more about myself, my career, and my motivation to become an online trainer.

Preview 03:50

In this lecture we're going to look at the theory behind .NET memory management. What exactly is Garbage Collection, and how does it work?

Introduction To .NET Memory Management
04:17

Many lectures in this course contain source code examples. Feel free to download the code and follow along. And here's the good news: it doesn't matter if you have a Window, Mac or Linux computer. The code will run on all three operating systems.

In this lecture I demonstrate how my solutions and projects run on all operating systems. I will show you how to build and run the source code on a Mac, on Linux and in Visual Studio running on Windows 8.

At the end of this lecture you will have learned that .NET code is portable and can run on at least five different operating systems.

Preview 03:19

Background Info - About Me
01:22
+
Fundamentals Of The .NET Framework
7 Lectures 31:15

Welcome to the Fundamentals of .NET Memory Management section. I will give a quick introduction on how the section is organised before we get started.

Section Introduction
00:43

This lesson will lay down some groundwork and explain in detail what the stack is for, and how data on the stack is laid out in memory.

After completing this lecture you will have learned which data is stored on the stack and what happens when this data goes out of scope. Finally, you will have seen a Stack Overflow exception, and know how to avoid this exception in your own code.

The Stack
05:23

This lesson will lay down some groundwork and explain in detail what the heap is for, and how data on the heap is laid out in memory.

After completing this lecture you will have learned which data is stored on the heap and what happens when variables that refer to this data go out of scope. Finally, you will be able to explain what the 'Garbage collection' process does.

The Heap
04:59

A popular question that often comes up in job interviews is: "what is the difference between a value type and a reference type?"

This lecture looks at value types in detail. What are value types, how do they store their data, and what happens when value types are assigned or compared? After completing this lecture you will be able to answer all of these questions with ease.

Value Types
05:42

A popular question that often comes up in job interviews is: "what is the difference between a value type and a reference type?"

This lecture looks at reference types in detail. What are reference types, how do they store their data, and what happens when reference types are assigned or compared? After completing this lecture you will be able to answer all of these questions with ease.

Reference Types
05:29

Value types and reference types are both derived from the same common base class "Object". This is a nice feature of the .NET Framework, but behind the scenes it creates some problems for the runtime environment because reference types can never exist on the stack.

In this lecture you will learn how .NET uses a cool trick called "Boxing" to get around this problem. After completing the lecture you will be able to explain what boxing and unboxing is, and why it is neccesary.

Preview 06:04

Congratulations on finishing this section. This is a recap of what we have learned.

Section Recap
02:55
+
A Detailed Look At Garbage Collection
6 Lectures 01:14:46

Welcome to the Detailed Look At Garbage Collection section. I will give a quick introduction on how the section is organised before we get started.

Section Introduction
01:48

The .NET Garbage Collector is responsible for cleaning up all dereferenced objects on the heap. In this lecture we are going to take a long hard look at its internal architecture.

I will show you how the Garbage Collector uses a mark-and-sweep cycle to periodically clean the heap, how it uses generations to sort objects into short-living and long-living groups, and how it is specifically optimized for handling large objects.

After completing this lecture you will be able to describe in detail how the Garbage Collector works. You will also be aware of the assumptions it makes about object sizes and lifetimes.

Garbage Collection In .NET
16:46

In this lecture we're going to look at a couple of specific performance improvements you can make to your code, that will help you work with the Garbage Collector instead of against it.

We will look in detail at the assumptions the Garbage Collector makes about the size and lifetime of the objects in your code, and how you can take advantage of these assumptions by carefully designing your objects.

After completing this lecture you will have learned how to write fast and efficient code that works in harmony with the Garbage Collector.

How To Optimise Your Code For Garbage Collection
18:20

In this lecture we are going to take a closer look at finalisers, which are special methods that are called by the .NET Garbage Collector just before an object gets deallocated. Finalisers offer a very convenient safety net for last-minute deallocation and disposing of resources held by the object.

But finalisers are dangerous and very hard to get right. I will show you three big disadvantages of finalisers in terms of code robustness and memory performance.

Finalisers in .NET
17:11

Sometimes an object needs to access scarce operating system resources, like a graphics device handle, a database connection, or a block of unmanaged memory. The Dispose pattern is an efficient design pattern in .NET to quickly release these resources when they are no longer needed.

In his lecture you will learn why the dispose pattern is much more efficient than simply releasing the resources in the finaliser, and I will also teach you how to write a reference implementation of the design pattern.

Preview 16:34

Congratulations on finishing this section. This is a recap of what we have learned.

Section Recap
04:07

Garbage Collection
10 questions
+
Simple Tricks To Improve Memory Allocation In Your Code
8 Lectures 01:22:49

Welcome to the Simple Tricks section. I will give a quick introduction on how the section is organised before we get started.

Section Introduction
01:19

The .NET runtime uses a very cool trick called 'boxing' that allows you to use value types and reference types interchangeably in your code.

In this lecture I will use a test program to measure exact how much overhead the boxing operation introduces. You will see how to use the log profiler built into the Mono framework to track what the garbage collector is doing while the program is running.

After completing this lecture you will have learned why boxing is best avoided in mission-critical code.

Avoid Boxing And Unboxing
11:50

Strings in .NET are immutable. This can introduce a large memory footprint when modifying strings, especially when done in a loop.

In this lecture I'll show you a test program that builds a string of 10,000 characters using either simple string concatenation, or the StringBuilder class. We will investigate exactly how much heap memory is allocated, how often the garbage collector runs, and which methods are called in each scenario.

By the end of the lecture you will have learned why you should always use a StringBuilder to modify strings in mission critical code.

Do Not Concatenate Strings
16:09

Structs in C# are Value Types, which means they are allocated inline with other data, either on the stack or on the heap. This can save a lot of memory, especially if you quickly need to allocate millions of items.

In this lecture I will show you a test program that uses structs and classes to store a large amount of data. The struct- and class-based implementations will go head to head to determine which has the smallest memory footprint.

By the end of the lecture you will have learned in which scenarios a struct is the best choice for storing data.

Use Structs Instead Of Classes
17:55

Collections and lists in .NET automatically resize their internal buffer to accommodate the addition of new items. This is a very nice feature because it means you never have to worry about the size of your lists.

But successive resizes can litter the heap with dereferenced storage buffers and inflate the memory footprint of your code. I will demonstrate a program that fills a list with items and ends up allocating much more memory than it actually needs.

By the end of this lecture you will have learned a very simple trick to keep the memory allocation of lists and collections firmly under control.

Preview 12:30

LINQ is a nice feature of the C# language that lets you access enumerated data with a flexible and powerful query language. LINQ queries can access any data that supports the IEnumerable interface. But thoughtless use of LINQ can produce a large memory overhead and can significantly slow down your code.

In this lecture I will show you a spellchecker that uses LINQ and allocates way too much memory. Then I'll demonstrate a simple change that has a dramatic impact on the memory footprint.

Avoid Calling ToList In LINQ Expressions
18:04

Background Info - Can We Modify Strings Directly?
01:10

Congratulations on finishing this section. This is a recap of what we have learned.

Section Recap
03:52

Memory Allocation Tricks
10 questions
+
Final Words
1 Lecture 01:44

In this lecture I would like to thank you for finishing the course and offer some final words.

Course Recap
01:44
About the Instructor
Mark Farragher
4.4 Average rating
773 Reviews
5,013 Students
10 Courses
Microsoft Certified Trainer - 700+ reviews, 4.5 rating

My IT career spans 2 decades and I've worn many different hats over the years. I am a serial entrepreneur and have launched two startups in The Netherlands. 

I started using C# and the .NET framework 15 years ago. Today I live in Barcelona and spend my time creating online courses to share my knowledge of professional C# programming with a wide audience.

I have active MCSA and MCSD certifications plus legacy MCAD and MCT certifications. You can verify my credentials on my LinkedIn profile. 

Feel free to check out my course ratings before enrolling. As of today I have 9 published courses, 600+ reviews, and a recent average course rating of 4.41.