|
-
Jul 28th, 2004, 02:05 PM
#1
setting to nothing or using dispose or both?
When should use use dispose? or implement IDisposable versus setting an object to nothing?
what is the most effective way to destroy a created object and free up its used memory?
-
Jul 28th, 2004, 02:20 PM
#2
Frenzied Member
in theory, you don't have to do any of the above. once the Garbage collector goes around, it will detect objects that aren't needed and blow them out. The problem is, you don't WHEN he will come around (just like MY local garbage service).BUT, for some (me included), i like to set the objects to NOTHING at the end of the function.
-
Jul 28th, 2004, 02:33 PM
#3
then whats the point of the dispose method?
-
Jul 28th, 2004, 07:37 PM
#4
Frenzied Member
u got ME lol i've argued that with my co workers too. i still set my objects to nothing regardless. I will look into that cause it IS confusing.
-
Jul 28th, 2004, 07:40 PM
#5
Addicted Member
You use dispose when you are using unmanaged objects and need to clean them up manually.
Also it is good to use it when using valuable limited resources and check that they are free before the object is collected. (e.g print queue, database connections, which may be bad for performace if continually needing to wait for a timeout)
If there is no need in your class for these then don't implement IDisposable.
-
Jul 29th, 2004, 01:14 AM
#6
You should also use Dispose on GDI objects like Graphics and Bitmaps, brushes and pens.
Setting your variable to nothing just removes your reference to them, but the image is still in memory.
I have made it a habbit of always disposing an image variable before setting it to a new image, and I can see my memoryusage is reduced quite a bit.
If you only have a few images in your app it probably doesn't matter, but for image-intensive app's it's a must.
VB Code:
'Dispose the image if there allready is an image in memory.
'This has to be done with all bitmap objects to save memory.
If Not PictureBox1.Image Is Nothing Then PictureBox1.Image.Dispose
PictureBox1.Image=NewImage
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jul 29th, 2004, 03:19 AM
#7
Hyperactive Member
With Forms, I use three fixed line of code:
1) Form.close
2) Form.dispose
3) Form=nothing
What I can see is that after disposed , a form is not setted=nothing, I have to do it explicitly. If I well remember (I'm not completely sure), it's true also after a GC.Collect instruction.
Anyway, I've a lot of things to study, yet. Many behaviours of GC are a mistery for me!
Live long and prosper (Mr. Spock)
-
Jul 29th, 2004, 03:21 AM
#8
Member
Great question! I wanted to know about it as well for quite sometime. Good thinking guys!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|