Is it possible to completely dispose of a usercontrol in winrt? - c#

I have an app where I dynamically create user controls depending on the situation. When I'm done with them I remove them from the container, but they are still taking up memory.
I've implemented a "dispose" method in the code to clear the controls inside of the user control, but I still feel like there is a better way to do it.
I'm not sure what code I can post as no code is really relevant.
All I really have is what I'm doing now with the dispose method
public void Dispose()
{
ContentWebView.NavigateToString("about:blank");
YoutubeMediaPlayer.Stop();
}

The Dispose method is used for releasing unmanaged resources.
When all references to an object are removed, the garbage collector will eventually "Collect" it (freeing the memory). You can't actually control when this happens, even calling GC.Collect is technically just a suggestion. You can, however, invoke dispose on an object directly (note that this may cause odd behavior or exceptions if it is still in use!)
If you suspect a control has unmanaged resources that need to be cleaned up, you should call Dispose on it directly when you are finished with it (GDI Bitmap objects need to be cleaned up like this). From your comments, it sounds like you just need the garbage collector to actually free the memory, which is something thats hard to control (you can try calling GC.Collect though).
MSDN for Dispose: http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

Related

How to know if winform form has been garbage collected?

I'm using C# but probably the same in VB.NET. I C++ I would just set a break-point on an objects destructor to know when/if it was deleted/free'd. I understand that in winforms the base class calls SupressFinalize so that form destructors are never called, so I guess I can't do it that way. Is there another method to know if an object has been garbage collected? It seems like a catch-22 because if there was you would probably need a reference to check, but by holding that reference the garbage collected will not crush it.
I've read this What strategies and tools are useful for finding memory leaks in .NET?, and I understand there are tools and/or frameworks out there to handle this "big picture" and I'm sure in a few weeks I'll be trying out several of these methods. For now I just have a really strong feeling I might have a leak related to forms not being deleted, so just want to check this one thing (and I want to know just for the sake of knowing).
I know I can watch Dispose, but I'm pretty sure Dispose can be called but still end up with the form object still being around. To test that theory I created a known-issue where I registered for a callback event in my form, then closed the form without unregistering it. Sure enough, Dispose was called (and "disposing" was true), but later when the event was fired, it still hit my break-point inside the form that was supposedly disposed already.
There are really two issues here:
As for your original question, you can use a WeakReference to monitor an object's existence without affecting its lifetime.
Your underlying question suggests that you have a misunderstanding of what garbage collection is, and how it works. The point of garbage collection is that you should never care if an object has been collected or not. Instead, you should focus on the references to the object, and if they have been reassigned or made inaccessible from rooted references. Don't worry about the instance, worry about the references to it.
The entire concept of a managed language is that you don't need to care when an object is actually garbage collected. Lots and lots of time and effort go into the GC to make sure that it doesn't collect objects that it shouldn't, that it doesn't leave objects that it should collect (when it decides to do a pass over the generation that it's in) and that all of this is done reasonably efficiently. This means that if an object consumes a large amount of managed resources and also implements IDisposable (say, a DataTable or DataSet) is disposed it is still consuming a lat of memory, and disposing of it doesn't make it get garbage collected any quicker (although you should still dispose of it to ensure any managed resources go away).
The GC is built to work best when you leave it alone and let it do it's job rather than interfering with it by trying to, for example, manually cause collections to take place. This is occasionally useful for debugging purposes or learning about your program/the language, but it virtually never belongs in a production application.
Disposing has nothing to do with garbage collection or collecting an object. Disposing is the mechanism in place for dealing with a managed object that is holding onto an unmangaed resource (or another object that is holding onto an unmangaed resource). Disposing of the object is telling it to clear up that unmanaged resource, but it has nothing to do with the garbage collector freeing the managed resources of that object. The destructor is there so that you don't free the managed resources before freeing the unmanaged resources, but it's perfectly acceptable (and in fact should always happen) that the unmanaged resources are cleaned up (via dispose) before the managed resource is freed.
Now, it is still possible, given all of this, for a program to have a memory leak, but there are a few questions you really need to be asking yourself first. How large is the leak? Is it a one off, continuous over time, every time we do function X, etc? What would it take for the program to consume so much memory as to be disruptive (either run out of memory, cause other programs to run out of memory, etc) and is that likely to occur with common or legitimate uses of the program? Usually you don't need to start asking these types of questions until you start getting out of memory exceptions, or notice that you start running out of physical memory for a program that oughtn't to. If you notice these problems then you can start looking around for objects that implement IDisposable that aren't being disposed, to see if you're holding onto references to very large objects (or collections of objects) that you no longer need, etc.
Sorry for the wall of text.

Do I have to use a “using” statement in a short method?

Do I have to use a using statement to dispose immediately even in a method? Or will the ending of the method cause an automatic Dispose of all local variables including graphics?
(I’m asking this because I’ve seen examples that have Dispose calls at the end of methods, and wanted to know if that’s really necessary.)
Thanks.
Yes, you do. Going out of scope does not do anything; it does not call Dispose(), it does not garbage-collect, it does not call a finalizer.
If the type is IDisposable, then yes. It is your job to tidy up after yourself (assuming that the object is actually "done with" at this point).
Possible side-effects of not doing this:
files get left open and cause access-exceptions (FileStream)
connections get left open and cause pool saturation (DbConnection)
unmanaged handles get saturated and cause resource starvation (any winforms/etc)
transactions get left open and cause blocking (TransactionScope / DbTransaction)
etc
basically, bad things.
Furthermore, in most cases where you see Dispose() at the bottom of the method, a using would be preferable. There are some cases where that isn't possible (fields on an object, for example), but the point remains: it sounds like those are bad examples.
You don't ever have to dispose of an object. But if you want to release unmanaged resources earlier rather than whenever the GC gets around to it (which is when it processes the fReachable queue) then yes you should since the scope of of a method does not determine when a disposable object's finalize will be called.
For more you might want to read Jeffrey Richter's Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework
As delnan noted if there's a hard limit to the resources you're using you're much more likely to impacted if you let it get reclaimed by the GC rather than calling Dispose. DB connections are a good example of this kind of reasource and is certainly the reason why the NumberOfReclaimedConnections performance counter got created.
Yes, use using. It is a great statement.
Actually if you don't call Dispose (either manually or by using using), you can end up in a situation where your managed memory is stil quite empty, but unmanaged resources are depleted. Eventually it can cause a deadlock because other objects or other processes will wait for your unmanaged resources and you will wait for them. (Garbage collector won't run, because managed memory still won't be full.)
So please call Dispose as soon as you can. Once you don't need an object which has got that method defined, call it.

When do I need to use dispose() on graphics?

I'm learning to draw stuff in C# and I keep seeing recommendations to use dispose(), but I don't quite understand what it does.
When should I be using dispose() on a
code-drawn graphic?
What happens if I
don't?
Do I need to call it every
time a graphic is not visible, such
as on a GUI that has tabs and the
user switched to the other tab, and
then redraw it when they switch back?
Will I break things if I call it when I shouldn't?
Will Batman escape the evil clutches of the Joker?
When should I be using dispose() on a code-drawn graphic? Whenever you are completely done with any object that implements IDisposable, you should call Dispose just before it becomes eligible for garbage collection. As others have pointed out, it's best to use the using statement instead of calling Dispose directly.
What happens if I don't? Your program will be slightly less efficient because it will take up slightly more resources than necessary. This is the only drawback to not disposing graphics; however, not disposing other classes can actually cause errors (one famous example is StreamWriter). For this reason, it's best to always dispose any class that implements IDisposable, as a general rule.
Do I need to call it every time a graphic is not visible, such as on a GUI that has tabs and the user switched to the other tab, and then redraw it when they switch back? No. Objects should only be disposed when you are completely done with them.
Will I break things if I call it when I shouldn't? Yes. If you call Dispose before you are done with the object, and then attempt to use the disposed object, you will get an ObjectDisposedException.
Will Batman escape the evil clutches of the Joker? Tune in tomorrow, same bat-time, same bat-channel!
When you ask for a graphical object, Windows will allocate a bit of memory for you. Calling dispose will tidy up that memory for you. If you do not call dispose, all of these handles to memory wil remain open and eventually your system will run out of resources, become slower and eventually stop (closing the program may free them however).
Because you're using .NET, when you have finished using your graphics object, the garbage collector will eventually call dispose for you. The problem with the garbage collector is you never know when it will clean up the object, so it may leave these resources open for longer than necessary.
This said, you should never have to call dispose yourself. Far better will be to put your object in using scope:
using(Graphics g)
{
// do something with the resource
}
Now when you leave this using scope, the object will be destroyed and dispose will be called automatically for you. You should put all objects that have the dispose method defined inside a using scope.
In noob speak, Dispose() is about cleaning up after you're done using an unmanaged resource.
What's an unmanaged resource? It's all the stuff that CLR doesn't manage for you. They're thing like file handles, database connections, network sockets, GDI+ pens, etc. You get access to those things through a typical .NET object, but it will implement IDisposable, to allow you to clean up properly.
Why clean up? Until you've cleaned up after yourself, that resource isn't available by other parts of the program for use. In that respect, you're breaking things, because you're hogging a resource.
Why do this yourself? You should do this yourself as soon as you stop needing the resource, rather than rely on the auto-magic of the garbage collector, because it could take a long (well, unspecified) amount of time before the garbage collector gets to it. Until an object has been disposed of properly, you can't reuse the underlying resource, so you program will not function reliably.
Mitch Wheat says - Always call Dispose() on any object that implements IDisposable. GDI handles used by graphics objects are unmanaged and require disposing when you are finished with them.

What is the point of Finalize and Dispose methods in .NET? (see details before answering)

I get the need to clean up resources during the teardown of an object, but I have always found the differences between Dispose, Finalize, and the destructor methods a bit confusing.
I found this great article that concisely describes the distinctions between them, that I am going to have to save for future reference:
"Difference between Destructor, Dispose and Finalize methods" - Sanjay Saini
http://sanjaysainitech.blogspot.com/2007/06/difference-between-destructor-dispose.html
The fundamental question I am trying to ask here is this.
If a language offers destructors (for example C# [refuted]) what
value do Dispose and
Finalize add to the equation?
Am I just a curmudgeon that is used to the old school way of doing everything in the destructor, or is there something I am missing that is only possible by breaking the tear-down of an object into three parts?
UPDATE:
As noted in some of the replies, C# does not actually have destructors. The question may be moot at this point in recognition of that. When I read in the above referenced article that C# actually had a separate deconstructor (an error apparently), it threw me for a loop and I started wondering what the point of Dispose and Finalize would be if you had a final destructor to wrap up everything. I suppose that in a GC langauge like C# the concept of a single destructor to provide the denemount for an object doesn't make much sense.
Sorry for the downvotes on some of you guys, but a couple people didn't read the question carefully and thought I was asking about the difference between Dispose and Finalize, which really wasn't the point.
The author of that blog post is a bit confused...
In C#, there is no such thing as a "destructor". Only Finalizers and IDisposable.
The ~ClassName() method is not called a "destructor". It is called a finalizer.
Dispose exists to release resources from code, where the finalizer exists to be called from the GC. Very often, the finalizer calls the Dispose() method, but the "Dispose Pattern" sets you up to only handle unmanaged resources from the finalizer.
You see, when the finalizer gets called, you are on a different thread, and any managed object you have is not necessarily valid. Because of this, if you call Dispose() from the finalizer, you should really be calling Dispose(false) which tells the "Dispose Pattern" to only dispose unmanaged resources.
Further, the "Dispose Pattern" suggests that when Dispose(true) is called, you should suppress the finalizer on that object.
Only managed objects can be finalized automatically. If you want to offer implicit disposal of unmanaged objects, the Finalizer can be used. If you want to offer explicit control of disposal to a caller of your object, you can allow them to call Dispose.
I like this article.
It's hard answering this since you already link to articles that explains the difference.
But let me try anyway.
With garbage collection you have non-deterministic memory management, and since GC will run your finalizer, you have guaranteed, but non-deterministic resource management.
This is good, in the sense that you know things will be cleaned up.
However, it is bad, since you don't know when it will happen.
For instance, if you open a file and lock it while you have it open, not knowing when the file will become available to open again later on is bad, but the guarantee that at some point it will be closed is good.
Dispose and IDisposable serves that bad part by adding deterministic resource management. You choose when to dispose of your resources, closing the file, the network connection, the database connection, whatever. It's your choice, and you will typically dispose of the resource when you no longer need it. Thus, when you no longer need the file, you dispose of the object keeping it open. The object will stay in memory (non-deterministic memory management) for a while longer, but the file will be closed when you say so, ready to be opened again right away.
Thus you gain deterministic handling of resources, and couple that with non-deterministic memory management and you got the best of two worlds.
What looks like a destructor in C# (~Foo()) is really just an different way of spelling "Finalize".
Perhaps in hindsight the choice of syntax wasn't the best because ~Foo() in C# is rather different from ~Foo() in C++.
Sorry to answer my own question, but I think I have assembled from a variety of sources, including some of the answers a concise answer to my question addressing the differences in object lifecycles when transitioning to a GC platform.
Non GC Object Lifecycle:
Initialization: Happens in the constructor which is called when the first reference is added to object.
Cleanup: Happens in destructor which is called when last variable referencing object goes out of scope or is explicitly set to nothing/null/another object.
GC Object Lifecycle:
Initialization: Happens in the constructor which is called when the first reference is added to object.
Clean-up (first pass): Happens in the Dispose Method, which must be explicitly called by the client or implicitly called at the end of a Using statement. This clean-up is specifcially to free up resources that must be released immediately and not wait on the GC.
Clean-up (Final Pass): Happens in Finalize Method which is called on the GC's timetable, which can be delayed. If the finalize method is implemented, it is called unless the GC.SuppressFinalize has been called previously on the object (normally from within the dispose method) to flag it as already cleaned up.
Did I get this right? Feel free to edit if I missed something here.
c# does not offer destructors, so your question is somewhat moot?
I'm not sure on the reasoning between Destructors and Finalizers, but Dispose exists so that you can free the resources before the Garbage Collector gets to the object (which may be never!)
The problem with waiting for the finalizer to run to release resources, is that it does not run in a predictable deterministic manner. The finalizer is called when the GC is ready to destroy the object, and this can occur at anytime in your application.
This is why you have dispose. It allows you to deterministicly release resources at a known point in time, ensuring that resources will be freed up. Otherwise you have no way of controlling when the finalizer will run.
Finalize cannot access managed objects, so managed object should be handled in Dispose. The difference between those two is that you should use Dispose for managed and Finalize for unmanaged. Your Destructor should be calling your Dispose so you've properly cleared memory. Take a look at the following articles:
Code Project - IDisposable: What Your Mother Never Told You About Resource Deallocation
Gil Fink - Memory Leak And The IDisposable Pattern

Resources that have to be manually cleaned up in C#?

What resources have to be manually cleaned up in C# and what are the consequences of not doing so?
For example, say I have the following code:
myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
// Use Brush
If I don't clean up the brush using the dispose method, I'm assuming the garbage collector frees the memory used at program termination? Is this correct?
What other resources do I need to manually clean up?
If you don't dispose something, it'll be cleaned up when the garbage collector notices that there are no more references to it in your code, which may be after some time. For something like that, it doesn't really matter, but for an open file it probably does.
In general, if something has a Dispose method, you should call it when you've finished with it, or, if you can, wrap it up in a using statement:
using (SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black))
{
// use myBrush
}
Handles to internal windows data structures.
Database connections.
File handles.
Network connections.
COM/OLE references.
The list goes on.
It's important to call Dispose or even better yet, use the using pattern.
using (SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black))
{
// use myBrush
}
If you don't dispose something, it'll be cleaned up when the garbage collector notices that there are no more references to it, which may be after some time.
In the case of System.Drawing.Brush, Windows will keep internal windows structures for the brush loaded in memory until all programs release their handle.
The consequences of not disposing your IDisposables can vary from a negligible performance hit to crashing your app.
The Brush object in your example will be cleaned up by the GC when it feels like it. But your program won't have had the benefit of that bit of extra memory you would have gained by cleaning it up earlier. If you are using a lot of Brush objects this might become significant. The GC is also more efficient at cleaning up objects if they haven't been around very long, because it is a generational garbage collector.
On the other hand, the consequences of not disposing database connection objects could mean you run out of pooled database connections very quickly and cause your app to crash.
Either use
using (new DisposableThing...
{
...
}
Or, if you need to hold on to a reference to an IDisposable in your object for its lifetime, implement IDisposable on your object and call the IDisposable's Dispose method.
class MyClass : IDisposable
{
private IDisposable disposableThing;
public void DoStuffThatRequiresHavingAReferenceToDisposableThing() { ... }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
//etc... (see IDisposable on msdn)
}
Generally, anything that implements IDisposable should cause you to pause and research the resource you're using.
GC only happens when there's memory pressure, so you cannot predict when. Although an unload of the AppDomain will certainly trigger it.
Technically anything that inherits from IDisposable should be proactively disposed. You can use the 'using' statement to make things easier.
http://msdn.microsoft.com/en-us/library/yh598w02.aspx
Sometimes you will see inconsistent use of IDisposable derived objects in documentation sample code as well as code that is generated by tools (i.e. visual studio).
What's nice about IDisposable is that it gives you the ability to proactively release the underlying unmanaged resource. Sometimes you really want to do this - think network connections and file resources for example.
As others have said, using is your friend.
I wrote this blog entry about how to implement IDisposable in a fairly straightforward way that is less error-prone by factoring out the parts that are the most important.
A trick I use when I can't remember whether a given object is a disposable resource is to type ".Dispose" (at most!) after the declaration to get Intellisense to check for me:
MemoryStream ms = new MemoryStream().Dispose
Then delete the .Dispose and use the using() directive:
using(MemoryStream ms = new MemoryStream())
{
...
}
Well, as long as you use the managed version of the resources and don't call the windows APIs by yourself, you should be OK. Only worry about having to delete/destroy a resource when what you get is an IntPtr, as "windows handles" (and a whole lot other things) are known in .NET, and not an object.
By the way, the resource (as any other .NET object) will be flagged for collection as soon as you leave the current context, so if you create the Brush inside a method, it will be flagged when you exit it.
If it's managed (i.e. part of the framework) you don't need to worry about it. If it implements IDisposable just wrap it in a using block.
If you want to use unmanaged resources then you need to read up on finalisers and implementing IDisposable yourself.
There's a lot more detail under this question
First upon program termination, you can assume that memory used by the process will be eliminated with the process itself.
While using dispose or destructor in.net, one must understand that the time of when the dispose function is called by the GC is non-deterministic. That why it is recommended to use the using or calling the dispose explicitly.
When using resources such as files, memory objects such as semaphors and resources that live outside of the managed world of .net must be freed.
The SolidBrush for example, you need to dispose because it is a GDI object and living outside of the .net world.
The garbage collector does not only free up at program termination, otherwise it would not be really useful (on any decent/recent OS, when the process exits, all its memory is cleaned up automatically by the OS anyway).
One of the big advantage of C# compared to C/C++ is that you don't have to care about freeing allocated objects (most of the time at least); the gc does it when the runtime decides (various strategies when/how to do it).
Many ressources are not taken care of by the gc: file, thread-related ressources (locks), network connections, etc...
One place to be careful is Objects that look small to GC but are not... In the SharePoint API for example, the SPWeb object has a small footprint as far as the GC is concerned and so will have low priority for collection, but it has really grabbed a bunch of memory (in the heap I believe) that the GC doesn't know about. You will run into some fun memory issues if you are foreaching a whole bunch of these for example, always remember to use using or dispose!
Rather than thinking of an object as "holding" resources that need to be released, it's better to think in terms of an object as having altered something (possibly outside the computer!) which will outlive it, in a way could be harmful if it not undone or "cleaned up", but which only the object can clean up. While this alteration commonly takes the form of some concrete object in a pool being marked "busy", its precise form doesn't matter. What matters is that the changes need to be undone, and the object holds information necessary to do that.
The garbage collector will handle any managed resources. In your example, the brush will be cleaned up when the garbage collector decides to, which will happen some time after the last reference to the brush is no longer valid.
There are certain things that need to be manually cleaned up, but those are pointers retrieved from unmanaged sources, such as DLL calls, nothing within the .NET Framework needs this treatment however.

Categories

Resources