E
E
Evgeny Ivanov2021-08-05 07:42:10
C++ / C#
Evgeny Ivanov, 2021-08-05 07:42:10

How are objects destroyed and do we need to destroy them ourselves?

Here is a sample code.

public static string GetLastFolderName(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Directory.Name;
}

The code creates a new instance of the class.

Unlike Delphi, the issue of object destruction in C# is not considered, at least at the initial and intermediate level of learning the language. They say that everything is done automatically.
(in Delphi we do it ourselves, "by hand" and often if you forget to delete an object, this leads to errors)

How does the .NET virtual machine understand that it's time to destroy the object? And when does it happen?
After all, I can access it at any time, for example, launch an application in C # and in a day will access it from another service / application.

What happens if you need to execute the code above in a loop? That is, many objects will be created.
I don't think I need them anymore. As soon as I got return (folder name), I don't need this object.
And if all this is in a cycle, it would be logical to immediately destroy the object.

How are objects destroyed and do we need to destroy them ourselves?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
U
up, 2021-08-05
@logpol32

CLRium #5: Garbage Collector. 2019
Expands understanding of how GC works, memory allocation and usage, etc. But without first reading the relevant material, for example, from Richter, it will be difficult to understand.

V
Vasily Bannikov, 2021-08-05
@vabka

How does the .NET VM know when it's time to destroy an object? And when does it happen?

You can read more about the garbage collector here:
https://habr.com/ru/post/125968/ (not the most up-to-date article, but generally ok)
https://habr.com/ru/company/clrium/blog/463293 /
https://github.com/dotnet/coreclr/blob/master/Docu...
In short, it keeps track of all references to objects, and if no one can access the object, it destroys it.
The exact moment in time of object destruction in the general case is difficult to name.
What happens if you need to execute the code above in a loop? That is, many objects will be created.
I don't think I need them anymore. As soon as I got return (folder name), I don't need this object.
And if all this is in a cycle, it would be logical to immediately destroy the object.

In this case, the GC will collect them very quickly, since the only reference to this object was in a variable in this method.
You don't have to do anything on your own.
There will be no memory leak.
True, if this code will be executed very often, and it is a hot spot - it makes sense to abandon FileInfo, and parse paths without littering the heap.

A
Alexander Ananiev, 2021-08-05
@SaNNy32

https://habr.com/ru/post/125968/

V
V Sh., 2021-08-05
@JuniorNoobie

Let's just say that it is not necessary to do this (garbage collection yourself), but if it is very necessary (for example, within the framework of performance), then you can also prescribe the destruction of objects yourself (in finally, for example).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question