Home > .Net, C# > What is Finalize ? or Dispose vs Finalization. When they occur?

What is Finalize ? or Dispose vs Finalization. When they occur?

Finalization is a mechanism offered by the CLR that allows an object to perform some graceful cleanup prior to the garbage collector reclaiming the object’s memory.

The Finalize method is incredibly useful because it ensures that native resources aren’t leaked when managed objects have their memory reclaimed. However, the problem with the Finalize method is there is no guarantee of when it will be called, and because it isn’t a public method, a user of the class can’t call it explicitly.

The capability to deterministically dispose of or close an object is frequently useful when you’re working with managed types that wrap native resources such as files, database connections, and bitmaps. For example, you might want to open a database connection, query some records, and close the database connection—you wouldn’t want the database connection to stay open until the next garbage collection occurs, especially because the next garbage collection could occur hours or even days after you retrieve the database records.

What Causes Finalize Methods to Be Called?

1)Generation 0 is full

2)Code explicitly calls

System.GC’s static Collect method

3)Windows is reporting low memory conditions

4)The CLR is unloading an AppDomain

5)The CLR is shutting down

Categories: .Net, C#
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: