Forum Xamarin.Forms

Nullifying objects inside a method

KyuubiKyuubi USMember ✭✭
edited March 31 in Xamarin.Forms

Greetings,

I need clarification whether it is absolutely necessary to nullify the variable instances created or boxed within a method?

For example:

`

    public void Method(Instance instanceParameter)
        {
                var instance = new Instance();
                // or
                var instance2 = (Instance)instanceParameter;
                // After usage of the instance is it necessary to null the instance as like below?
                instance = null;
                instance2 = null;
        }

`

This is such a drag. Any help would be highly appreciated.

Thanks.

Posts

  • GraverobberGraverobber Member ✭✭✭
    edited March 31

    Hi @Kyuubi

    no this is not necessary.
    once you're done working on your method local variables they are "waste" and garbage collection will take care of it.
    You can read more about garbage collection here
    https://docs.microsoft.com/de-de/dotnet/standard/garbage-collection/
    https://www.geeksforgeeks.org/garbage-collection-in-c-sharp-dot-net-framework/

  • JohnHardmanJohnHardman GBUniversity admin

    @Kyuubi

    As long as the objects do not have unmanaged resources associated with them, then you do not need to do anything to clean up at the end of methods, so no need to null them. The garbage collector will take care of them. See https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals

    However, if those objects have unmanaged resources (e.g. operating system file handles) associated with them, then you are likely to want to use the Dispose pattern to ensure that the unmanaged resources are released. See https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/unmanaged

  • NMackayNMackay GBInsider, University admin

    You don't have to but they will get garbage collected faster as they have been finalized, if it was a list of many objects I would suggest it's not a bad thing but it will get collected eventually assuming it doesn't have a strong reference.

    If it was VB6 code...yes you would have to :)

  • JohnHardmanJohnHardman GBUniversity admin
    edited April 1

    @NMackay said:
    You don't have to but they will get garbage collected faster as they have been finalized

    If an object is no longer required part way through a method, you might want to null out the reference to avoid the object being promoted by the garbage collector (although there are also reasons why you might not want to do this - more complicated code, having to make C# objects nullable if using C# 8, potentially making debugging more awkward, the compiler might realise the object is no longer required anyway). However, I'm not sure that nulling out a reference to an object where that reference is about to go out of scope anyway (as per the original question) has any value (I'm assuming C# here, pretending not to know anything about VB6!). Happy to be corrected if I'm wrong on that :-)

  • NMackayNMackay GBInsider, University admin

    Don't agree with all of these points having spent years memory profiling Xamarin apps but the scope point in the initial point I agree, I deviated somewhat.

Sign In or Register to comment.