Re[9]: Работа - с чего начать: С++ или С#?
От: romangr Россия  
Дата: 25.04.09 04:24
Оценка: 3 (1)
Здравствуйте, catBasilio, Вы писали:

B>
B>static void Main(string[] args)
B>{
B>MyObject[] manyObjects = new MyObjects[10000000]; 

B>Console.WriteLine(manyObjects[1]);

B>...
B>// многа кода
B>}
B>


B>неиспользуемые объекты будут держаться до заврешения программы.

B>И попробуй убеди меня что это не утечка памяти. И я очень сомневаюсь gc при вызове "ручками" поймет что это некому не нажный код и удалит его.

B>P.S. это высосанный из пальца пример, но проблему он показывает вполне.


Этот высосанный из пальца пример показывает, что ты не знаешь ни c#, ни то как работает gc.

Читаем C# Language Specification:

3.9 Automatic memory management
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 (§10.13) (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.


Так что твой жуткий массив может быть собран мусорщиком сразу после Console.WriteLine. Кстати, по примеру, у тебя весь массив — массив null-ов.
... << RSDN@Home 1.2.0 alpha 4 rev. 1089>>
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.