Answer the question
In order to leave comments, you need to log in
Where can I find code that demonstrates the usefulness of garbage collection with C#?
Much has been written about garbage collection. But basically it is written how to correctly carry out garbage collection, in what cases it happens automatically, etc. But it is difficult to find an example that clearly shows its usefulness. In principle, it is clear that the memory is freed up, the program works faster, etc. But I would like something practical and visual, as in advertising.
For example:
Piece of code 1. We do not collect garbage. Runtime 3 minutes.
Piece of code 2 (rewritten piece of code 1). We collect garbage. Execution time became 10 seconds.
Please tell me if there are such examples?
PS On many other issues in books on programming and manuals, they also sin. They tell how to do it, but do not focus enough on why to do it and what benefits a developer can get from using this or that feature.
Answer the question
In order to leave comments, you need to log in
In principle, it is clear that memory is freed up, the program runs faster, etc.
Where can I find code that demonstrates the usefulness of garbage collection with C#?
Perhaps a little not about that, but at one time I was puzzled by the question "And what kind of using should I?". I came across a good article on it and about the garbage collector too.
1 - there is no such code (see p3)
2 - they don’t really sin in books / networks, rather, you still don’t understand the explanations of what and when to do
3 - it’s really possible to assess whether it is necessary to interfere with garbage collection, you can only profile a specific project, in terms of memory loading, and in terms of responsiveness, at a certain (rather high) intensity of requests (user or server)
for typical desktop applications, a pause when the user looks at the screen between mouse clicks (pressing keys) and realizes the reaction of the program is more than enough for cleaning up garbage
until you encounter a situation of large threaded processing (or large request streams in the case of a service), which require a significant amount of memory at each iteration / request - you will not be able to assess the need (or lack thereof) for manual intervention with garbage collection
ps
Например:
Кусок кода 1. Мусор не собираем. Время выполнения 3 минуты.
Кусок кода 2 (переписанный кусок кода 1). Собираем мусор. Время выполнения стало 10 секунд.
if you have a computer with 4/8/16 GB of RAM, and the task consumes 10-100 .. 200-300 MB - the garbage collector will not interfere at all (unless, of course, the memory is not full and so to the eyeballs) .. and for some 10 microseconds will clean up the garbage while you look at a word like Finished and realize its meaning .. something like this
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question