CATEGORII DOCUMENTE |
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:
When the object is created, memory for an object is allocated.
When the object is no longer in use, the object becomes eligible for garbage collection.
During garbage collection, the destructor (10.12) for the object (if any) is run, and the object's associated memory is freed.
The garbage collector maintains information about object usage, and uses this information to make memory management decisions, such as where in memory to locate a newly created object, when to reclaim the memory occupied by an object that is no longer in use, and when to relocate an object. Like other languages that assume the existence of a garbage collector, C# is designed so that the garbage collector may implement a wide range of memory management policies. For instance, C# does not require that objects be garbage collected as soon as they are eligible for collection, or that objects be garbage collected in any particular order.
For example, the program
using System;
class A
}
class B
~B()
}
class Test
}
creates an instance of class A and an instance of class B. These objects become eligible for garbage collection when the variable b is assigned the value null, since after this time it is impossible for any user-written code to access them. The output could be either
Destruct instance of A
Destruct instance of B
or
Destruct instance of B
Destruct instance of A
because the language imposes no constraints on the order in which objects are garbage collected.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 801
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved