473,943 Members | 18,482 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
Hi Niki,

Have a look at this, and follow as well the shortlink to nothing.

http://msdn.microsoft.com/library/de...vastmerase.asp

I hope this helps?

Cor

Jul 21 '05 #11
Cor Ligthert <no**********@p lanet.nl> wrote:
Constructive quoting again from you, I once gave you a sample that more
people can do that however decent people do not do that.

Leaving the message from Scott out from this makes it the same as if Scott
was telling that the Tower Bridge was in Paris.
Sorry if you thought I was implying that - I certainly wasn't, and I
don't actually think anyone thought I was.
The message you give the same answer again as Scott was saying, while you
give the idea that he did not.


No, it didn't. It gave more information, notably that the GC is capable
of detecting variables after their last use within a method in most
situations - it *automatically* makes objects (where appropriate)
eligible for garbage collection before the end of the method. This is
not what Scott implied (in the way that Niki and I read it at least)
when Scott wrote:

"There are certain circumstances though, when you may want the object
in question to fall out of scope immediately, rather than waiting for
the end of a procedure."

To me, that suggests that unless you take the given action, the method
(procedure in Scott's terminology) has to end before the variable's
contents will be collected.

However, I suspect that this whole subthread isn't really about garbage
collection at all, and that you'd have praised Scott for adding extra
information if the posts had been the other way round... (And that if
it had been someone else replying to Scott, you wouldn't have posted at
all.)

--
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 #12
Is there anything equivalent to Erase in C#?

Sarfraz
"Brian Davis" <br***@knowdotn et.nospam.com> wrote in message
news:OL******** ******@TK2MSFTN GP11.phx.gbl...
In addition, VB has an Erase statement, which should deallocate memory used for the array's elements.

Brian Davis
http://www.knowdotnet.com

"Scott M." <s-***@nospam.nosp am> wrote in message
news:eS******** ********@TK2MSF TNGP09.phx.gbl. ..
Everything in .NET is a type (either reference or value types). All types are managed by the Garbage Collector. There is no need to "free up

memory"
as this is the purpose of the GC. There are certain circumstances though, when you may want the object in question to fall out of scope immediately, rather than waiting for the end of a procedure. In this case you could

make
the object = Nothing.
"Sarfraz Hooda" <sh****@iqueri. com> wrote in message
news:Od******** ******@TK2MSFTN GP09.phx.gbl...
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 #13
Brian Davis <br***@knowdotn et.nospam.com> wrote:
In addition, VB has an Erase statement, which should deallocate memory used
for the array's elements.


I don't *believe* it does, actually. It just sets each array element to
Nothing, which is the equivalent of calling Array.Clear. That's
different from actually deallocating the memory, which is the garbage
collector's job.

--
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 #14

I would tend to agree with you. What made me think otherwise is that the
MSDN documentation states that the Erase Statement is "Used to release array
variables and deallocate the memory used for their elements," so I am not
really sure. If I had to guess, I'd say that you are correct, and that MSDN
just means that it is marked as OK for deallocation by the GC rather than
actually deallocated.

As a side note, Erase isn't the same as calling Clear. Clear will set
elements of the array to Nothing, while Erase sets the array variable itself
to Nothing.

Either way, setting the array to Nothing should be functionally the same as
calling Erase, but, as you have pointed out in previous posts, that
shouldn't be necessary in most applications.

Brian Davis
http://www.knowdotnet.com

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Brian Davis <br***@knowdotn et.nospam.com> wrote:
In addition, VB has an Erase statement, which should deallocate memory used for the array's elements.


I don't *believe* it does, actually. It just sets each array element to
Nothing, which is the equivalent of calling Array.Clear. That's
different from actually deallocating the memory, which is the garbage
collector's job.

--
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 #15
Brian Davis <br***@knowdotn et.nospam.com> wrote:
I would tend to agree with you. What made me think otherwise is that the
MSDN documentation states that the Erase Statement is "Used to release array
variables and deallocate the memory used for their elements," so I am not
really sure. If I had to guess, I'd say that you are correct, and that MSDN
just means that it is marked as OK for deallocation by the GC rather than
actually deallocated.
It wasn't even marking things as OK for deallocation in my (wrong)
understanding. It was just setting things to Nothing inside the array,
which may well not make them eligible for GC, as there could be other
references.

The MSDN is unfortunately not very clear (or, I believe, accurate) on
this. Looking at the IL generated, I believe it does just set the
variable to Nothing, which certainly doesn't fit in with the docs you
quoted. Odd.

So

Erase Foo, Bar Baz

is just the same as

Foo = Nothing
Bar = Nothing
Baz = Nothing

It seems relatively useless to me, to be honest. Maybe it was more
useful in VB6.
As a side note, Erase isn't the same as calling Clear. Clear will set
elements of the array to Nothing, while Erase sets the array variable itself
to Nothing.
Yup. You're absolutely right - I had completely misread the spec.
Sincere apologies for confusing things further :(
Either way, setting the array to Nothing should be functionally the same as
calling Erase, but, as you have pointed out in previous posts, that
shouldn't be necessary in most applications.


Yup.

--
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 #16
Hi Jon,

I would have answered as well as someone else had written the message you
did, however they seldom do. (Probably the tone had been something different
because arguing between us is not something new, therefore I did leave out
the introduction.)

:-)

In my opinion Scott told it very clear. I did not read anything in your
message that would have given the OP more information when this arguing had
not been.

In my opinion there is a scene where setting an array to nothing can be good
and I do not believe that that is contradicting what Scott wrote.

In pseudo to make it language independent

Arraylist MyArray

Main
MakeNewArray

MakeNewArray
MyArray = New ArrayList
for i=0;i<5000000;i ++ Arraylist.add(m yobject)

Event Button1Click
MyArray = nothing

Event Button2Click
MakeNewArray

In my opinion I give the GC the change to clean up the old MyArrayObjects
before the program is ended.

Cor
Jul 21 '05 #17
Sarfraz Hooda <sh****@iqueri. com> wrote:
Is there anything equivalent to Erase in C#?


There's an equivalent to what Erase *actually* does, which is just
setting the variable to null:

object[] foo = new object[100];
....

foo = null;

This is very rarely useful, as discussed elsewhere in the thread. (See
also http://blogs.msdn.com/csharpfaq/arch.../26/97229.aspx )

Of course, setting an instance or static variable to null is slightly
different - but for instance variables, I usually find that the array
is useful for exactly the same length of time as the object itself, so
again there's rarely a point in setting the variable to null.

Note that Erase doesn't automatically make an array eligible for
garbage collection - if there's another reference to the same array, it
can't be collected until that reference has changed (to null or to a
different array) or become eligible for garbage collection itself.

--
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 #18
Cor Ligthert <no**********@p lanet.nl> wrote:
In my opinion Scott told it very clear. I did not read anything in your
message that would have given the OP more information when this arguing had
not been.
Really. Having read only Scott's post, when would you believe Foo was
eligible for garbage collection in the Sub below?

Public Shared Sub Test()
Dim Foo(9) As Integer

Console.WriteLi ne(Foo(0))
Console.WriteLi ne("Hello")

MessageBox.Show ("Can it be collected now?")

End Sub

If I had just read Scott's post (and didn't know anything else) I would
expect the array not to be eligible for garbage collection until the
end of the Sub. In fact (in release mode), it's eligible for garbage
collection immediately after Foo(0) has been evaluated.
In my opinion there is a scene where setting an array to nothing can be good
and I do not believe that that is contradicting what Scott wrote.


No, and that's also not contradicting anything I wrote. I didn't
contradict what Scott wrote - I added to it.

--
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 #19
Hi Jon,
Really. Having read only Scott's post, when would you believe Foo was
eligible for garbage collection.
This is the message from Scott
Everything in .NET is a type (either reference or value types). All types are managed by the Garbage Collector. There is no need to "free up memory" as this is the purpose of the GC. There are certain circumstances though, when you may want the object in question to fall out of scope immediately, rather than waiting for the end of a procedure. In this case you could make the object = Nothing.


Where Scott did write about the place it was eligible for the GC?

When the method falls out of scope, it does not always mean that an (in that
method created) object is collectable for the GC, however Scott did not
write that either.

This was your answerUsually that's unnecessary, however, as the garbage collector can tell
when a variable is last used in the IL. Unless you're in a particularly
special case (eg where a variable is used in the first iteration of a
long loop, and not thereafter) it's best not to clutter up your code
setting variables to null/Nothing.


It did, at that moment you wrote it, in my opinion Add nothing to the
question of the OP:

"How to destroy arrays", your message tells in a way where an array is
destroyed by the program (not for the system).

And that was for me already clear in the explanation from Scott. Why would
it be set to nothing at the end of the procedure (method)

Cor
Jul 21 '05 #20

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
10143
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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,...
0
11539
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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...
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.