Call on list.removeAll

I am new to C # programming and the GC concept and its realities for IDisposable are still a bit vague. What does calling Dispose mean in terms of garbage collection? Specifically, I am wondering if the following code might fail, depending on when the garbage collection occurs (I was unable to crash it during my tests).

//List<TestClass2> tc2List;
//TestClass2 invokes a thread. It implements IDisposable. 
//Its Dispose() sets a stop-condition for the thread,
//and joins the thread, awaiting it to stop. (may take 100 msek)

tc2List.RemoveAll(t =>
{
  if (String.Compare(t.Name, "Orange") == 0)
  {
    t.Dispose(); //May take up to 100 msek
    return true;
  }
  return false;
});

      

+3


source to share


4 answers


Your code works, but that's bad style. Predicates shouldn't have side effects. Therefore, you must first remove the items and then remove them.



Predicate<T> filter = t => t.Name == "Orange";
foreach(var t in tc2List.Where(filter))
  t.Dispose();
tc2List.RemoveAll(filter);

      

+4


source


I am wondering if the following code might fail to fail, depending on when the garbage collection exits at

No it won't fail



//Its Dispose() sets a stop-condition for the thread,
//and joins the thread, awaiting it to stop. (may take 100 msek)

      

This is a bit of a typical use of Dispose (), but not a mistake. A more efficient approach would be to use a different Stop()

one so that you can stop all threads at once. Or call Dispose()

from Parallel.ForEach()

. But whichever method you choose doesn't get in the way, nor does the GC get in the way.

+2


source


Do you have a finalize method TestClass2

?

Eliminate basic properties

  • This must be implemented in classes that implement the IDispose interface.
  • This is a good place to release unmanaged resources such as file, descriptors and connections, etc.
  • The Dispose () method is called explicitly in the code itself.
  • The dispose () method is automatically called (for objects that implement IDispose) when used in "use"

Refer to the link .

+1


source


What does calling Dispose mean in the sense of garbage collection?

Calling dispose means that you are forcibly clearing things (literally memory) using your implementation of the Dispose method. Note. (Some of the framework classes call your Dispose implementation for you. For example, System.Windows.Forms.Form)

Garbage collection in contrast is a feature of the .NET runtime that will automatically clean you up. By automatic I mean as it depends on memory pressure and other factors that take care of the runtime.

Simple strategy that I would recommend. Do Dispose () if you think the memory will stay longer than necessary and will affect the application. Else leave it at runtime, it will automatically clean up (i.e. terminate) objects when they go out of scope. The GC has its own algorithm for how to clean up, but you can rely more on it.

Next question Specifically, I am wondering if the following code might sometimes break, depending on when the garbage collection is triggered at

I think no. It depends on how you write the TestClass2 finalizer which code is faulty due to the use of GC. Definitely your t.Dispose () call will not conflict with the GC.

0


source







All Articles