4

In my app I have a large object that's created every few seconds. I do with it some job and then I don't need it anymore.

I saw in the task manager that the ram size goes up even if I don't have any reference to the object and it needs to be collected.

After implementing IDisposable the ram goes down immediately.

Why is this? I didn't do GC.Collect, I just released the object and told the GC it doesn't need to call the finalizer for my object.

EDIT:

Here is the code I use for my IDisposable objects:

public void Dispose()
{
  base.Dispose();
  Dispose(true);
  GC.SuppressFinalize();
}

private void Dispose(bool disposing)
{
  //here i release unmanaged resources
  if (disposing)
  {
    //here i release all managed resources
  }
}

~MyObject()
{
  Dispose(false);
}

In my large object, I do myObject.Dispose(); after I don't need it anymore.

My question was not about how to implement IDisposable or how GC works in general. I just want to know how is it possible that there's a difference when I dispose of the object myself or let GC do its job.

5
  • GC.Collect is very problematic or performance issue creator. GC is itself smart to kick in when ever it needs to.
    – Zenwalker
    Jul 6, 2012 at 9:08
  • 1
    Please show the code of this 'big' object and the implementation of the Dispose method. Just having an IDisposable on an object does not magically release memory, since the CLR and GC do not any specific handling for IDisposable objects. Only the Finalizer method has a special meaning.
    – Steven
    Jul 6, 2012 at 9:20
  • "and then the ram goes down immideitly". What method do you use to analyse that the used RAM goes down?
    – Steven
    Jul 6, 2012 at 9:48
  • @zenwalker i know this I dont do GC.Collect Jul 6, 2012 at 12:10
  • 1
    Is that the exact code of your Dispose(bool disposing) method, or do you actually have code on the line //here i release unmanaged resources ?
    – Steven
    Jul 6, 2012 at 12:12

4 Answers 4

1

With .NET memory doesn't have 2 states as usual (used and unused), but in effect 4: (A) Used by live object, (B) Used by dead object, (C) Not used by any object, but by the framework and (D) not used.

When you create an object, the Framework will first try to use memory of category (C) for it, is there isn't enough usable left of it, it will ask the OS for some of type (D), will convert it into (C) and then use it for your object.

When an object goes out of scope, it will fall from (A) to (B), on the next garbage run it will go from (B) to either (C) or (D). This depends heavily on the internal structures (think memeory fragmentation and friends), of the memory pressure and of the type of object collected (think IDisposable and friends)

What you want to achieve is, that as fast as possible after your big Object goes out of scope, the memory it used goes to (D). Here are some hints:

  • Make sure your object's size is a multiple of 4K - this makes it much more likely, that no other object shares a memory page with your object, so it can more easily be given back to the OS

  • Experiment with a AddMemoryPressure() bracket around object disposal, but be aware of side effects. This is similar to forcing GC.Collect(), but less intrusive: It will hint the GC to collect soon, but not exactly now

  • Rethink your basic concept: Is the big object a Singleton (i.e. only one can exist at any given time)? If yes, think of allocating it once and recycling it: This will make your Applications RAM requirements more predictable - and possible save you from ugly OOM situations while running.

4
  • Thanks for the answer! about the first bullet, how to do that? and its really something that help release memory? 2.I prefer not use it. 3.Its not singleton and eally i have not option to change the all app design Jul 6, 2012 at 12:15
  • There are at least three categories of objects: rooted, unreachable, and freachable (objects which are reachable via the Finalization Queue, but not via any other rooted reference). Only objects which are completely unreachable are eligible for destruction. Those which are freachable will, if registered for finalization, moved from the Finalization Queue to the Freachable Queue; the system will then at its leisure run the Finalize methods for all objects in the Freachable queue.
    – supercat
    Jul 6, 2012 at 15:01
  • @supercat From the POV of the developer, rooted is the same as Used by live object, my (A), unreachable is Used by dead object, my (B), freachable and other not-yet-freeable-to-the-OS parts are Not used by any object, but by the framework, my (C). So your classification is very similar to mine, with the only difference being, that your angle is how the framework handles the memory, while mine is how the developer can handle it. Jul 6, 2012 at 15:11
  • @EugenRieck: Objects which are freachable need to be regarded as a category of their own; the system guarantees that their members will be accessible (meaning that accessing such members will cause the underlying code to execute, not that such code will necessarily do anything useful), and it's possible for such objects to be "resurrected".
    – supercat
    Jul 6, 2012 at 15:22
1

As @Steven pointed out in his comments, IDisposable is something the CLR doesn't care about. By implementing it you simply tell consumers of your object to call its Dispose method when object no longer needed. In terms of mem management you can make up your own interface for this, say IDisposable2, and get the same technical effect. This would however be a stupid thing to do because .net developers should know to call idisposable.dispose when object no longer needed. Also, there is built in language (c#) support for this interface (using {...}).

You write "I tried to implement IDisposable and then the ram goes down immideitly."

What matters is what your "implement" does. The code there should clean up unmanaged code, windows resources etc.

0
0

Using IDisposable means that you are implementing Dispose method where you put all your clean up code for the resources that you dont need anymore. It does not remove your object to the managed heap, the GC will still be the one responsible for freeing up the memory.

-2

When you implement IDisposable, and dispose the object, you are marking the object as available to be garbage-collected.

You can't predict exactly when it will be collected though.

1
  • 4
    This is not correct. IDisposable has no meaning for the CLR and GC. Calling dispose will not mark the object as available to be garbage collected. Only when an object is not rooted anymore is an reason for the GC to collect that object.
    – Steven
    Jul 6, 2012 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.