473,943 Members | 18,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to destroy arrays

Hi,

I have created an array of Objects in a collection. I was wondering is there
a way to destroy the array to free up the space in the memory ? or they are
automatically destroyed and garbagge collected by .Net framework?

Sarfraz
Jul 21 '05
43 2738
So then, would this make sense:

Sub Foo
Dim x as new FooFoo()
x.stuff()
'Done using x now, but don't want to wait for dispose to naturally run
x.dispose()

...some operation that will take time...
...some operation that will take time...
...some operation that will take time...
...some operation that will take time...

End Sub
Now, in this case, if there were clean up code in x's dispose method we
could control when it happens, rather than waiting for the GC to get around
to it?

-Scott

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Scott M. <s-***@nospam.nosp am> wrote:
First, you and Cor have been busy little bees on this....


:)
Second, my original reply (OR) and the "certain circumstances" that I was referring to have more to do with an object's Dispose() method firing and not so much about that object's eligibility for garbage collection.


Ah, right. Read on...
My understanding on this (and please correct me if I'm wrong) is that when an object falls out of scope, it will fire off its Dispose() method (if it has one).


No, that's not true. Dispose is only called if *something* calls it -
it's not automatic in most situations.

Now, C# has the "using" statement which *does* automatically call
Dispose at the end of the block, but that's basically just syntactic
sugar. Most finalizers call Dispose themselves, of course.
Then, when the object is actually about to be removed from memory
by the GC (which we can't say with any certainty if and when that happens), the object's Finalize method will fire.


Assuming it has one, and that GC.SuppressFina lize hasn't been called
for that object, yes.
My understanding has been that while an object may be eligible for garbage collection, if it hasn't fallen out of scope yet, then its Dispose method might not have fired yet. By setting the object to Nothing before the end of the procedure, you can get the Dispose method to fire sooner, rather than later. This can be very useful when the object is holding some expensive resource open or locked. So by getting Dispose to fire sooner, the
resources can be cleaned up sooner.

Is this not correct?


Nope - see above. It's certainly a good idea to call Dispose as soon as
you're able to (though no sooner, of course!) but that needs to be done
explicitly.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #41
Scott M. <s-***@nospam.nosp am> wrote:
So then, would this make sense:

Sub Foo
Dim x as new FooFoo()
x.stuff()
'Done using x now, but don't want to wait for dispose to naturally run
x.dispose()

...some operation that will take time...
...some operation that will take time...
...some operation that will take time...
...some operation that will take time...

End Sub
Yup. (Assuming that FooFoo implements IDisposable, of course.)

If you don't call Dispose yourself, it could be a *very* long time
before resources are cleaned up - especially if the object is in
generation 2 (very unlikely in this case, but speaking about a more
general situation).
Now, in this case, if there were clean up code in x's dispose method we
could control when it happens, rather than waiting for the GC to get around
to it?


Yes. IDisposable should only be implemented for types either directly
containing unmanaged resources (handles etc) or for types which wrap
other types implementing IDisposable (eg StreamWriter, which wraps a
Stream).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #42
Ok Joh, thanks.

-Scott
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Scott M. <s-***@nospam.nosp am> wrote:
So then, would this make sense:

Sub Foo
Dim x as new FooFoo()
x.stuff()
'Done using x now, but don't want to wait for dispose to naturally run x.dispose()

...some operation that will take time...
...some operation that will take time...
...some operation that will take time...
...some operation that will take time...

End Sub


Yup. (Assuming that FooFoo implements IDisposable, of course.)

If you don't call Dispose yourself, it could be a *very* long time
before resources are cleaned up - especially if the object is in
generation 2 (very unlikely in this case, but speaking about a more
general situation).
Now, in this case, if there were clean up code in x's dispose method we
could control when it happens, rather than waiting for the GC to get around to it?


Yes. IDisposable should only be implemented for types either directly
containing unmanaged resources (handles etc) or for types which wrap
other types implementing IDisposable (eg StreamWriter, which wraps a
Stream).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #43
Hi Scott,

Have a look at these pages, it has lot information about the GC and
describes it in my opinion very clear.

It as well describe dispose, however I saw in these newsgroups that the idea
about it is turning (also by Microsoft people), it is telling the old way
which even says that you have (when you follow the text) to dispose
everything by code that has a dispose method. Every control by instance has
it, however in my opinion is the dispose of that done by the component
class, which the control implements. About the dispose I have in general
probably the same idea as Jon now.

(In addition, please do not catch me on a word; in such a small message, not
everything is right)

However the pages are great, I got the link from Jay B. Harlow

http://msdn.microsoft.com/architectu...l/scalenet.asp

Cor

Jul 21 '05 #44

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
26292
by: Rob | last post by:
My first GUI so be gentle... When I start my program I call a class that runs the initial window. While in this class if a certain button is pressed it calls a function outside the class. This function then initially calls another function to "root.destroy()". Basically I want the current window gone so the function I just called can open it's own window. The problem I'm stuck with is that once this function is done and I need to close...
6
18024
by: max(01)* | last post by:
hi people. when i create a widget, such as a toplevel window, and then i destroy it, how can i test that it has been destroyed? the problem is that even after it has been destroyed, the instance still exists and has a tkinter name, so testing for None is not feasible: >>> import Tkinter >>> fin = None >>> fin1 = Tkinter.Toplevel()
2
6294
by: Ook | last post by:
I was taught that in a copy constructor, you don't have to destroy your arrays, but in an overloaded assignment operator, you have to. Example: When do you delete, and when do you not? Is it arbitrary, or are there general guidelines that should be followed? I'm thinking that in the copy constructor, you are creating a new instance of the class, and in the assignment, you have already created the class and therefore have to destroy...
8
1958
by: vvenk | last post by:
Hello: I just wrote my first ASP.Net application. It worked fine on my machine and when I put into production, the ASP.Net process reaches 50% quite fast and then the system does not work anymore until I kill that process. Obviously, this is not acceptable. Looking back, I do not destroy any objects in my form. Would that be the reasn why the application breaks down?
43
508
by: Sarfraz Hooda | last post by:
Hi, I have created an array of Objects in a collection. I was wondering is there a way to destroy the array to free up the space in the memory ? or they are automatically destroyed and garbagge collected by .Net framework? Sarfraz
2
1558
by: Flavio | last post by:
Hi, I have a miniframe composed mainly of combo boxes, that I need to destroy and recreate multiple time with different choice lists for the combo boxes. My problem is that even after destroying a miniframe with the Destroy() method, when it is recreated, the combo boxes show the same lists of its previous incarnation...
15
4654
by: Mark C | last post by:
I know a string is immutable, but is there any trick or any other way to destroy a string Thanks www.quiznetonline.com
6
4040
by: muppetjones | last post by:
I'm pretty new at this, and I'm trying to figure out how Perl's classes work with signals. Specifically, it doesn't seem that a class's DESTROY function is called when you Ctrl-C the program. I tried using use sigtrap qw(handler DESTROY INT QUIT);, but I'm not even sure this is the proper way to catch the signal. Either way, it seems I no longer receive a reference to my object when DESTROY is called. I keep getting this error: Can't...
3
6533
by: drzoo2 | last post by:
Completely noob question as I am not a programmer but really trying hard to learn Python (Object oriented programming in general). I am writing a program in python that calls a popup window with some general information with an ok button. If I close the window using the window's close button I have no problems but If I call the same destroy function using the button call, it will not kill the popup. def destroy(self, widget, data=None):...
0
9970
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
11303
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10666
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9866
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8228
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7393
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6312
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3516
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.