[Video] Learn Garbage Collector (Part -5) : Generational Garbage Collector

Hello All,

This is the last part in the series of videos on Garbage Collection process. I have posted four parts earlier and I hope if you’ll see all parts in the series, you’ll get basic end to end understanding of Garbage collection process.

In the first part, I discussed about the basics of Object creation and it’s life cycle. In second video, I included the Garbage Collection steps. Third part included the discussion on Finalization which is one of the important concepts of Garbage Collection Process. In the fourth part, I discussed on the Internals of Finalization. The links of earlier parts are

[Video] Learn Garbage Collector (Part -1) : Object Creation and it’s life cycle

[Video] Learn Garbage Collector (Part -2) : How Garbage Collector runs

[Video] Learn Garbage Collector (Part -3) : What is Finalization

[Video] Learn Garbage Collector (Part -4) : Internals of Finalization

In this post, I am discussing on Garbage Collection Optimization and discussed the Generations in details.

Hope you’ll enjoy it.

Cheers,
Brij

[Video] Learn Garbage Collector (Part -4) : Internals of Finalization

Today, I am going to post fourth part of the series on Garbage Collector. I have published three videos earlier. In first part of the series, I discussed about the basics of Object Creation and it’s life cycle. In second, I talked about the basics of Garbage Collector and How does is run and the steps involve in it. In the third part of the series, I discussed one of the very important concepts of Garbage Collector that is Finalization. Please find the links of earlier part below

[Video] Learn Garbage Collector (Part -1) : Object Creation and it’s life cycle

[Video] Learn Garbage Collector (Part -2) : How Garbage Collector runs

[Video] Learn Garbage Collector (Part -3) : What is Finalization

In today’s Video, I’ll be discussing about the Internals of Finalization. This is little complex but very interesting as well. One similar advice again, prefer watching earlier parts of the video before watching it.

Cheers,
Brij

[Video] Learn Garbage Collector (Part -3) : What is Finalization

Hello All,

This video is third part in the series on Garbage Collector. I have published two earlier parts of the video. In the first part of the video, we discussed how the object gets created, used and destroyed. There are multiple steps involved in it which you must know to understand the Garbage Collector. Please go to the first part of the using the below link.

[Video] Learn Garbage Collector (Part -1) : Object Creation and it’s life cycle

In second part of the video, I discussed about the Garbage Collector and its processing. I discussed the various steps during garbage collection. Please use the below link for second post.

[Video] Learn Garbage Collector (Part -2) : How Garbage Collector runs

In today’s post, I have discussed one of the important concepts of Garbage Collector Finalization and it’s importance. I’ll advise you watch the earlier parts of the video to gain most of the today’s video.

Cheers,
Brij

[Video] Learn Garbage Collector (Part -2) : How Garbage Collector runs

Hello All,

This is second part of the series on Garbage Collector. In my last post, I shared first part of the series. In that video, I talked about the basics of Object creation and talked about it’s life cycle. It is very important for understanding the whole Garbage Collection topic. You can find the link of last post below

[Video] Learn Garbage Collector (Part -1) : Object Creation and it’s life cycle

In today’s video, I have talked about when the Garbage Collector triggers and discussed about it’s processing.

Cheers,
Brij

[Video] Learn Garbage Collector (Part -1) : Object Creation and it’s life cycle

Hello All,

Garbage Collector is one the basic concepts in .NET framework and ironically one of most misunderstood concepts as well. Also, It is also one of the most asked interview questions. To understand the whole concept, one need to understand Object creation and it’s life cycle life cycle and memory management from the application point of view.

I am going to post a series of videos on Garbage Collector and hope this will help you in learning the basics of Garbage Collector. In my first video, I have talked on Object creation and about it’s life cycle.

Hope you’ll enjoy the video and don’t forget to share your feedback.

Cheers,
Brij

Passing a reference type using ref keyword

In this post, I am going to talk about ref keyword in C#. You all must be knowing the basic use of this keyword. But for them who are new to this keyword or C#, will explain the basics.

ref keyword is used to pass the parameter by reference. It means the parameter references the same memory location as the original variable.

As we know that there are two type of variable in C#. One is Value type and other is Reference type. Whenever a value type variable is passed to a method, a copy that variable is created and passed to the method. So let’s see it pictorially

Passed by valueSo when the variable is updated in the called method, then initial variable is not updated but the copy variable gets updated.

Now let’s pass the parameter using ref keyword. So when we pass a value type variable using ref keyword the reference of the variable is passed and both variable names points to same memory location. So when the variable is updated it is available in both method. It can be depicted pictorially as

Pass value using refTill now we passed the value type variable. But when we send other type (reference type) variable to the method the reference is passed so when we update the object in the called method, the updated object gets available to both the method. let’s see the example

For this example,I used an instance of Class type

Typeand the example is

refLet’s come to the real question. What if we pass the instance using ref keyword. When the instance is itself a reference type then what else we get from it. Or they exactly same? Let’s see by example

refref1But if we see the above example, then we get the difference that now variable name points the reference of the reference variable name. When we update the object it updates the same memory location as earlier. By seeing the above example, it seems that there is no difference between passing the reference type variable normally and by ref keyword. But Wait !! Let’s see the screenshot

refref12Now when the UpdateName method get’s executed then what will happen. Let’s see this

refref2

So here after execution of UpdateName both variable name and myName points to null and now the object is available in memory but not accessible from any variable.

But if we pass the object without ref keyword in same method then

ref2So here variable name points to null but myName still points to that object and object is accessible.

So it means as long as we update the object by updating it’s property method etc.,it is same in both case but when we play with the variable name then it makes a difference as we have seen in the above example.

So next time when someone asks that difference by passing the a reference using ref keyword or without ref keyword then you can explain it.

Happy learning!!

Regards,
Brij

Learning checked and unchecked context

Today, I am going to discuss about one of the rarely used but very useful keywords of C# that are called checked and unchecked context. I learnt this few days back.  These keywords can be used at certain scenarios and can save you from hazardous situations. This would be more useful when you are playing with lots of data and doing lots of operations over it.

So let’s start with a quiz. Just predict the result of the following code snippet

   int value = 5000000;
   int multiplier = 2500;
   int result = value * multiplier;
   Console.WriteLine(result);

What would be the result?
12500000000?   No

It would be -384901888. But how?

Because if you check the max value of int. It is 2147483647. Now you can see that the result of multiplication is higher than this max value. So you can think that the value displayed is min value of int. Which is certainly not desirable.

And even you would not get any overflow exception. So be cautious, while selecting the data types of variable whether it is a normal or temporary variable.

By default, Arithmetic overflow and underflow exception is turned off. You can turn it in on assembly level. For this, you can go to

Project->Properties->build tab-> advanced button. So you’ll get the following screen.

Here you can check the checkbox for arithmetic overflow/underflow and now if you run the code, you will get the OverflowException.

But this got enabled at assembly level and if you want to to have at some code snippet level then first uncheck the above marked checkbox and second you need to use the checked and unchecked keyword to have this feature.

So checked and unchecked keywords are provided for these scenarios. If you want to get exception if some values overflows or underflows.

  1
   int value = 5000000;
   int multiplier = 2500;
   int result = 0;
   checked
   {
   	result = value * multiplier;
   }

So if you run the above you’ll get the overflow exception again. What actually checked does, whenever a value is assigned to primitive variable, it the checks with the variable’s max value if the value is greater/lower than the max/min value, it throws overflow/underflow exception.

The above code  that is in checked block, can also be written as

int result = checked((int)(value * multiplier));

One need to be cautious while writing checked/unchecked keyword. This code would not throw any exception.

unchecked keyword also can be used if you don’t want to have a check for overflow/underflow. the code can be written as

            int value = 5000000;
            int multiplier = 2500;
            int result = 0;
            unchecked
            {
                result = value * multiplier;
            }
            Console.WriteLine(result);

If you have enabled overflow exception check at assembly level but don’t want it at certain situations then you can use unchecked keyword.

There are few key points, one need to remember-

  • In any case, you cannot rely on user input whether you have some validation or not. So it’s always recommended that you have this check and code for handling the overflow exception. Even if you are getting from some third party services etc, then also I would recommend this check.
  • You cannot rely on the following code, generally it does not throw overflow/underflow exception.
        static void Main(string[] args)
        {
        	checked
            {
            	addition();
            }
            Console.ReadKey();
        }
    
        private static void addition()
        {
        	int value = 5000000;
        	int multiplier = 2500;
        	int result = 0;
        	result = value * multiplier;
        	Console.WriteLine(result);
        }
    

    I mean, one should write code statements in checked block. If you are calling any method from the checked/unchecked block, you cannot sure that it work as desired.

  • These checked/unchecked context works on CLR primitive types only.
  • System.Decimal is c# primitive type but not CLR. So checked/unchecked context has no effect on this.

I hope you all have enjoyed this feature if didn’t learn it earlier.

Who already aware of this feature, please share some more points that can be added here.

Cheers,
Brij