Good day, dear HashKodovtsy. In the program that I need to write, I need to create more than a million copies of one class in the minimum amount of time. Naturally, no amount of operative is enough for this. This begs the question: how to manage the garbage collector? I need to call him in ODA. parts of the code. I will be glad to any links (not to search engines, but to articles checked by you) and your answers.

Thank you in advance.

ZY.: The code must be managed, only C #
ZY.2: A program with a similar count. instances, by definition, is not optimal. The fact is that these are the conditions of the job. That is why I ask you not to consider alternative solutions.
ZY.3: Any recommendations and advice on how to avoid stack overflow are welcome.
З.Ы.4: Any links regarding work with large numbers are welcome.

2 answers 2

When something like that was needed on one of the forums, they answered me like this:

The amount of memory is determined by the fields of the object and the fields of its ancestors. Plus, for each object, the CLR creates an additional 2 fields: Type object pointer and Sync block index. Richter’s book (CLR via C #) describes in detail how to create instances of a class. It is impossible to force an object. But it is possible to release the resources occupied by the object - the Dispose pattern.

If properties are used in your class, you can try the delayed initialization technique, with this technique some members of the class are not initialized until required. More here

    And what have the garbage collector?

    • he is by definition uncontrollable
    • why do you call him?
    • it all depends on the size of the instance, if a million of them fit in the size of the process allowed in the system, then there will be no problems with the creation, even if there are not enough operatives, well, it will take you a minute to think, scream heavily, the time dimension is not your concern
    • the condition of the problem does not include the storage of the instance, delete it immediately after creation :)
    • Read about the differences management and unmanagement code in .NET - uzumaxy
    • In C #, there is no possibility to get direct access to memory, so there is no analogue to the C ++ delete operator. This is a garbage collector, it is precisely the poetmo that is not needed. You can manage the garbage collector, but the code will be unsafe, which is not the case. I am interested in exactly what the nbit user indicated above. Thanks to him. - uzumaxy
    • If you try to create such a count. objects, the stack overflow will simply pop up. Need to approach differently. - uzumaxy
    • Rather, OutOfMemoryException, and not because of lack of memory, but because of the lack of space in the address space. - AlexeyM