wicked programmer
helping developers everywhere innovate and learn
  • Home
  • Events
  • Publications
  • Learning
  • About

General

IDisposable and The Finalize Method

Wicked Programmer January 1, 2017

I have been working with .NET since 2001.  That is when I joined Microsoft and spent the next 10 years helping customers architect and build amazing applications on the .NET platform.  Often my role is to lead a team of developers helping them build amazing software.  Even though .NET has be around for many years, there are still developers that do not understand the use of IDisposable interface and the Finalize method.  Proper understanding of the IDisposable interface and the Finalize method is critical to creating a good .NET application.

#1 – .NET manages memory for you

.NET provides a feature known as automatic memory management.  Memory allocated by .NET [aka managed memory] does not need to be explicitly released. This frees developers from the tedious and difficult task of releasing memory.

Except for advanced scenarios, you should not be trying to manage memory.  .NET manages memory for you through the use of garbage collection.  The following article has a simple explanation of the garbage collector.

“The .NET garbage collector manages the allocation and release of memory for your application.  Each time you create a new object, the common language runtime allocates memory for that object.  The runtime continues to allocate memory for new objects.  However, memory is not infinite.  Eventually the garbage collector must perform a collection to free some memory.  The garbage collector will determine the best time to perform a collection based on the allocations being made.  That time is nondeterministic.  When a collection occurs, the garbage collector will checks for objects that are no longer being used by your application and performs the necessary operations to reclaim memory.” – Garbage Collection, MSDN

What does this mean for you?  Easy!  You do nothing to manage memory because it is managed for you by the .NET garbage collector.  Hence the term “managed memory”.

#2 – Know thy “unmanaged resource”

For the majority of the objects that you create [in .NET] you can rely on the .NET Framework’s garbage collector to handle memory management.  While .NET manages memory for you, there are situations where you will use resources that are not allocated by .NET.  These resources are referred to as “unmanaged resources”.  Unmanaged resources are not allocated or managed by the .NET runtime.

The most common types of unmanaged resources are objects that wrap operating system resources, such as files, window handles, and network connections.  Another common unmanaged resources used by developers are database connections.  Two classes that use unmanaged resources are SqlConnection and FileStream.  Each time you create and use an instance of either of these classes you are allocating unmanaged resources.

One potential way to identify a class that uses unmanaged resources is if they implement the IDisposable interface.  The IDisposable interface provides a mechanism for releasing unmanaged resources.  Unfortunately a class can use unmanaged resources without implementing IDisposable.  Fortunately most classes in .NET that use unmanaged resources implement the IDisposable interface.

#3 – You are responsible for releasing unmanaged resources

The garbage collector was specifically designed to release managed memory and does not know about unmanaged resources.  That means that anything not controlled by the garbage collector should be explicitly released.

So how does one explicitly release unmanaged resources?

The Common Language Runtime (CLR) provides a mechanism known as the Finalize method.  The Finalize method (aka finalizer) provides a mechanism for objects to free resources and perform other cleanup operations before an object is reclaimed by the garbage collector.  Any object that implements a finalizer is eligible to have their Finalize method called by the garbage collector.  The garage collector will call the finalizer under the following conditions.

  • When an objects is determined to be inaccessible by the garbage collector and the object has not become exempt for finalization by a prior call to the GC.SuppressFinalize method.
  • During the shutdown of an application domain, unless the object is exempt from finalization.  Any object that is accessible will have their Finalize method called at shutdown.

There are many limitations to using the Finalize method (see Object.Finalize for more details).  The biggest limitation is that the time when the Finalize method is called is nondeterministic.  To ensure deterministic release of unmanaged resources you need to provide an implementation of the IDisposable.Dispose method.  When a developer calls your Dispose method it is your responsibility to release any unmanaged resources.

Summary

After reading this post you should come away with three key takeaways.

  • .NET manages memory for you and you do not need to do anything to help the garbage collector or to manage memory.
  • There are resources that are not managed by the .NET runtime.  These resources are referred to as unmanaged resources.
  • It is your responsibility to explicitly release unmanaged resources preferably by calling IDisposable.Dispose from your code.

This is the foundation from which we will build best practices for the proper use and implementation of the IDisposable interface and Finalize method.  We will cover at least two more articles, “Myths About Garbage Collection” and “Proper Implementation of IDisposable”.

Resources

These articles were used in the creation of this blog post.  I suggest that you read them once you are done reading this series.

  • Dispose Pattern
  • Implementing Finalize and Dispose to Clean Up Unmanaged Resources
  • System.IDisposable
  • Object.Finalize Method()

General

Hurray For Service Discovery–Part 1

Wicked Programmer April 6, 2016

There have been many times where I didn’t understand the need for service discovery products.  Often services are discovered for you, either behind load balancers or using traditional DNS lookups.  Why does one need a service discovery product?

Currently we are working on a machine learning and scientific computing product that spawns workers to fulfill requests.  These workers were receiving their work through a queue, an AWS SQS queue.  We had a situation where the initial connection to SQS was taking a very long time adding significant latency.  That set us off on a path to optimize the heck out of SQS.  Optimize, optimize, optimize!  It got much better, but there was still tons of latency, especially on the initial connection.

Knowing that SQS is limited in how much we can optimize away, we started looking at alternatives.  Unfortunately there is no really good alternative which is “cloud native”.  There are products like RabbitMQ that can be setup to handle this, but then you are paying for multiple EC2 instances to be always running.  When you architect for cloud one needs to architect for cost.  We want our cake and the ability to eat it too.  We only want to pay for what we use.

Problem mostly solved!  We have a great solution that we are in the midst of testing which negates the use of SQS all together,  This solution is based on service discovery which allows incoming requests to discover workers and dispatch work appropriately.  All of this without the need for additional infrastructure.

General

Being An Independent Consultant

Wicked Programmer March 30, 2016

Over the years I have held many roles, software engineer, lead developer, architect, and yes, consultant.  I have worked for large companies, small companies, startups, and even my own companies.  Recently I was faced with a decision to stay working at a company versus the excitement of going off and doing my own thing.  I am exciting to say that I decided to be an independent consultant again.  I made that decision 5 months ago.  I have been very busy since then and it has been an amazing journey so far.  I look forward to the many years to follow as I continue to build my brand and expertise.

Why am I excited to go off and do my own thing?

Over the years I have gained a reputation as being an expert on many different technologies and areas of development.  I am well know for my ability to write code in .NET and SQL.  My expertise spans many different types of applications including large scale web, high performance computing, cloud computing, mobile, and data centric applications.  I have used Visual Studio, Eclipse, XCode, Xamarin, WebStorm, IntelliJ, and many other development environments.  You might say that I am technology agnostic.  These days I find myself coding in many different type of languages such  .NET, Java, Scala, Objective-C, Swift, and JavaScript.

Gaining this experience is easy to do if you are someone like me that likes to learn new things.  The drive to constantly learn always keeps me using the latest and greatest technologies.  The passion for wanting to build great software is what fuels that drive to learn.  This passion is what accelerated my career and got me opportunities early on.  It is what got me jobs as a variety of startups, like Law Manager and Dash.com, and at large companies, like Microsoft and MITRE.

Shout out to Microsoft!

I would be remiss if I didn’t take the time to thank Microsoft.  I owe them a lot.  My 10 years at Microsoft has me as an Azure Insider, SQL Server Certified Master, Book Author with Addison Wesley, Magazine Author with Software Development Times, and much more.  I have spoken at TechEd, Financial Services Developer Conference, Healthcare Developer Conference, Code Camps, and much more.  Thank you Microsoft!  I look forward to continuing to use your great products and foster Azure, SQL Server, and many other great tools and services you provide.

Looking forward to joining TopTal!

As a leader, entrepreneur, developer, engineer, architect and consultant, I look to work with others that share my interests and abilities.  Others that I can contribute to, but also that can help me be better at what I do.  I have heard nothing but good things about Toptal (www.toptal.com) from people in my network.  They advertise that they represent top developers.  Having accomplished a great number of achievements as a developer, I look forward to joining an elite group and share my talents with them and their customers.  I am looking to join the Microsoft, Azure, AWS, and Web communities at Toptal.  This is a natural fit for me since I work on cloud-based software, on both Azure and AWS, and Microsoft products and technologies on a regular basis.

General

Hello mill5!

Wicked Programmer December 9, 2015

Hold onto your hats!  We have launched mill5.  Stay tuned…

«‹ 4 5 6 7›»







Back to Top

© wicked programmer 2025