2

In the following code, what happens to the memory myArray initially pointed to once it's reassigned in the 2nd line? Is that memory lost, or does the C# garbage collector take care of it?

static void Main(string[] args)
{
   double[] myArray = new Double[10];
   myArray = new Double[3];
}

As far as I've been reading, there's no way to explicitly free up the memory, so I'm hoping C# automatically does it.

2
  • 5
    garbage collector will take care of it Commented Jul 27, 2017 at 11:02
  • Data still remains in memory for a while. Because you don't have any pointer to that data anymore, GC will collect that space when necessary. Commented Jul 27, 2017 at 11:08

2 Answers 2

3

Of course, C# automatically releases memory that was associated with myArray prior to the assignment. This does not happen right away, but as soon as garbage collector realizes there are no remaining references to that Double[10] array object*, the memory allocated to the object gets reclaimed.

If you change your program slightly to create a second reference to the same array, like this

double[] myArray = new Double[10];
double[] myArray2 = myArray; // Second reference
myArray = new Double[3];

garbage collector would not release the object, as long as a reference to it remains accessible to your program.

* Sometimes your program finishes execution before garbage collector gets to complete its analysis. The memory still gets released, though.

Sign up to request clarification or add additional context in comments.

2 Comments

as soon as garbage collector realizes there are no remaining references .- depend on application size/using this can hapenned after application is closed
@Fabio That's fair. I added a footnote to mention this. Thanks!
2

When your variable goes out of scope and the memory is no longer required then it becomes eligible for garbage collection.

MS explains all, as seen here: https://msdn.microsoft.com/en-us/library/aa691138(v=vs.71).aspx

C# employs automatic memory management, which frees developers from manually allocating and freeing the memory occupied by objects.

Automatic memory management policies are implemented by a garbage collector. The memory management life cycle of an object is as follows:

  1. When the object is created, memory is allocated for it, the constructor is run, and the object is considered live.
  2. If the object, or any part of it, cannot be accessed by any possible continuation of execution, other than the running of destructors, the object is considered no longer in use, and it becomes eligible for destruction. The C# compiler and the garbage collector may choose to analyze code to determine which references to an object may be used in the future. For instance, if a local variable that is in scope is the only existing reference to an object, but that local variable is never referred to in any possible continuation of execution from the current execution point in the procedure, the garbage collector may (but is not required to) treat the object as no longer in use.
  3. Once the object is eligible for destruction, at some unspecified later time the destructor (Section 10.12) (if any) for the object is run. Unless overridden by explicit calls, the destructor for the object is run once only.
  4. Once the destructor for an object is run, if that object, or any part of it, cannot be accessed by any possible continuation of execution, including the running of destructors, the object is considered inaccessible and the object becomes eligible for collection.
  5. Finally, at some time after the object becomes eligible for collection, the garbage collector frees the memory associated with that object.

If you want to go into more detail then you can look here: https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/index

or I'm sure you can also find some blogs on the topic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.