C # Dispose () -completion

When i call object.Dispose(); Will the CLR immediately destroy an object from memory or mark an object to delete the next loop in it ?.

We call GC.SuppressFinalize()immediately after Dispose () whether this means: "Do not collect the object again for deletion because it has already been sent to the folder."

In fact, which generation is responsible for the destruction, I think generation 2.

+3
source share
4 answers

First of all, the IDisposable.DisposeGC is not about the same.

GC will clear the memory usage, IDisposable.Disposeused for deterministic free resources such as file descriptors, database connections, network connections, etc.

.

, , GC , . , , .

. .

, , , . , , , , , , , . , , .

GC.SuppressFinalize - , " , , , ".

IDisposable.Dispose, , , . GC , Dispose. , , Dispose, Dispose.

, Dispose . , , - , , , ( ), (, , , .)

"link", , , , , . , FileStream, , , . , , , P/Invoke, .

, , IDisposable.Dispose , Dispose GC.SuppressFinalize, , " , , ."

, Dispose, ( , , ..), .

, :

  • Dispose ( ). , GC .
  • , , - .
  • GC, .
  • , , , (, , ),

:

, , :

  • ? (, , , , ?)
  • ( , ). , , , . GC , .

, , , , .

+11

Dispose - CLR-, GC.SuppressFinalize. , Dispose GC.

Dispose GC.SuppressFinalize(this), GC , .
, .

+4

, Dispose . , GC.SuppressFinalize() Dispose, , . .

, , (, ), , Dispose using. , , GC . Dispose.

. , , .

, , GC, .

+3

, "" (2 - ). , 0 "" ( ) , .

This does not include objects of generation 0 with complex finalizers, since the finalists are launched in a separate thread and, after their completion, are assembled in the usual way. They do not get a promotion.

Collecting a Generation 2 object can take a very long time, since the GC may only need to collect Generation 0 objects to free up enough free space on the heap. To force a call to the full collection of GC.Collect ().

+1
source

Source: https://habr.com/ru/post/1739589/


All Articles