473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Array.Resize question

I have an array of objects. When I use Array.Resize<T>(ref Object,int
Newsize); and the newsize is smaller then what the array was
previously, are the resources allocated to the objects that are now
thown out of the array released properly by the CLI?

Sep 7 '06 #1
7 6390
Yes.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"heddy" <he*******@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
>I have an array of objects. When I use Array.Resize<T>(ref Object,int
Newsize); and the newsize is smaller then what the array was
previously, are the resources allocated to the objects that are now
thown out of the array released properly by the CLI?

Sep 7 '06 #2
Thanks, Kevin. I guess coming form a C/C++ background I tend to still
worry about memory management heh.

Sep 7 '06 #3
"Kevin Spencer" <uc*@ftc.govwrote in message
news:eJ**************@TK2MSFTNGP05.phx.gbl...
Yes.
Actually -- no. The objects will no longer be reachable through the array.
Only if there are no other references to those objects, will they be garbage
collected, and then only when you run low on memory. To free resources, you
still have to call Dispose() by hand on each object.

>
--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"heddy" <he*******@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
>>I have an array of objects. When I use Array.Resize<T>(ref Object,int
Newsize); and the newsize is smaller then what the array was
previously, are the resources allocated to the objects that are now
thown out of the array released properly by the CLI?


Sep 7 '06 #4
Actually -- no. The objects will no longer be reachable through the
array. Only if there are no other references to those objects, will they
be garbage collected, and then only when you run low on memory. To free
resources, you still have to call Dispose() by hand on each object.
Incorrect. First, the question was, "are the resources allocated to the
objects that are now thown out of the array released properly by the CLI?"
The answer is, "yes." They are "released properly by the CLI."

There are 2 distinct errors in your answer. First, your assertion that
Garbage Collection only occurs when the system is low on memory. This is not
true. Low memory is only one of several criteria used to determine when
Garbage Collection occurs. Here is a good article that explains the process:

http://www.csharphelp.com/archives2/archive297.html

The second errror is the statement "To free resources, you still have to
call Dispose() by hand on each object." First, most .Net classes do NOT
implement IDisposable, meaning, they do not HAVE a Dispose() method.
Apparently, they do not all need a Dispose() method. In fact, IDisposable is
an interface implemented for managed classes that use unmanaged resources,
or that use a lot of managed resources, and would benefit from quick Garbage
Collection. It is not implemented for most classes because .Net Garbage
Collection handles memory allocation quite well by itself in most cases.

I have written large-scale applications that allocate dozens of megabytes of
RAM at a time, and they manage memory perfectly.

As for arrays, an array can be an array of anything. So, the idea that
creating a new instance of an array (which is what resizing does) is going
to necessitate Garbage Collection is simply mistaken. An array of integers,
for example, will not need any Garbage Collection, because integers are
value types. With an array of objects, yes, if there is a reference to an
object (in the array) in the running app, that object will not be
Garbage-Collected until that reference is removed. But on the other hand, if
you are still referencing an object, you don't WANT it to be
Garbage-Collected; you are still using it.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:eg**************@TK2MSFTNGP04.phx.gbl...
"Kevin Spencer" <uc*@ftc.govwrote in message
news:eJ**************@TK2MSFTNGP05.phx.gbl...
>Yes.

Actually -- no. The objects will no longer be reachable through the
array. Only if there are no other references to those objects, will they
be garbage collected, and then only when you run low on memory. To free
resources, you still have to call Dispose() by hand on each object.

>>
--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"heddy" <he*******@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegr oups.com...
>>>I have an array of objects. When I use Array.Resize<T>(ref Object,int
Newsize); and the newsize is smaller then what the array was
previously, are the resources allocated to the objects that are now
thown out of the array released properly by the CLI?



Sep 8 '06 #5
"Kevin Spencer" <uc*@ftc.govwrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
>Actually -- no. The objects will no longer be reachable through the
array. Only if there are no other references to those objects, will they
be garbage collected, and then only when you run low on memory. To free
resources, you still have to call Dispose() by hand on each object.

Incorrect. First, the question was, "are the resources allocated to the
objects that are now thown out of the array released properly by the CLI?"
The answer is, "yes." They are "released properly by the CLI."

There are 2 distinct errors in your answer. First, your assertion that
Garbage Collection only occurs when the system is low on memory. This is
not true. Low memory is only one of several criteria used to determine
when Garbage Collection occurs. Here is a good article that explains the
process:

http://www.csharphelp.com/archives2/archive297.html
A very nice discussion of what GC does when it runs, but I didn't see
anything about what triggers it. If you're running GC for any reason other
than low memory (i.e. calling GC.Collect()), something is wrong.
>
The second errror is the statement "To free resources, you still have to
call Dispose() by hand on each object." First, most .Net classes do NOT
implement IDisposable, meaning, they do not HAVE a Dispose() method.
Apparently, they do not all need a Dispose() method. In fact, IDisposable
is an interface implemented for managed classes that use unmanaged
resources, or that use a lot of managed resources, and would benefit from
quick Garbage Collection. It is not implemented for most classes because
.Net Garbage Collection handles memory allocation quite well by itself in
most cases.
So yes, you admit that Dispose() is necessary for freeing any resources
other than memory (and often memory as well, if it's allocated outside the
CLR). Usually, the word resources is used to refer to exactly the things
Dispose() is needed for, if one meant "only memory" one would say memory
rather than resources. It is clearly important to the OP based on his
wording that someone tell him that Array.Resize() won't free unmanaged
resources.
>
I have written large-scale applications that allocate dozens of megabytes
of RAM at a time, and they manage memory perfectly.

As for arrays, an array can be an array of anything. So, the idea that
creating a new instance of an array (which is what resizing does) is going
to necessitate Garbage Collection is simply mistaken. An array of
integers, for example, will not need any Garbage Collection, because
integers are value types. With an array of objects, yes, if there is a
reference to an object (in the array) in the running app, that object will
not be Garbage-Collected until that reference is removed. But on the other
hand, if you are still referencing an object, you don't WANT it to be
Garbage-Collected; you are still using it.
The OP spoke specifically of an array of objects, not structs. But in any
case when the array is resized, the old storage for the array must be freed.
The storage for the array proper is GC-tracked. For arrays of int or any
other value type, it's the only GC handle. It can be quite large and it
must be garbage collected.
>
--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:eg**************@TK2MSFTNGP04.phx.gbl...
>"Kevin Spencer" <uc*@ftc.govwrote in message
news:eJ**************@TK2MSFTNGP05.phx.gbl...
>>Yes.

Actually -- no. The objects will no longer be reachable through the
array. Only if there are no other references to those objects, will they
be garbage collected, and then only when you run low on memory. To free
resources, you still have to call Dispose() by hand on each object.

>>>
--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"heddy" <he*******@gmail.comwrote in message
news:11*********************@b28g2000cwb.googleg roups.com...
I have an array of objects. When I use Array.Resize<T>(ref Object,int
Newsize); and the newsize is smaller then what the array was
previously, are the resources allocated to the objects that are now
thown out of the array released properly by the CLI?



Sep 8 '06 #6
A very nice discussion of what GC does when it runs, but I didn't see
anything about what triggers it. If you're running GC for any reason
other than low memory (i.e. calling GC.Collect()), something is wrong.
You must have overlooked the following (from the article I referenced):

"The garbage collector's optimizing engine determines the best time to
perform a collection, (the exact criteria is guarded by Microsoft) based
upon the allocations being made."

In other words, *you* don't run GC; it runs itself. A developer should
almost never have to do (and should almost never do) any manual Garbage
Collection.
So yes, you admit that Dispose() is necessary for freeing any resources
other than memory (and often memory as well, if it's allocated outside the
CLR). Usually, the word resources is used to refer to exactly the things
Dispose() is needed for, if one meant "only memory" one would say memory
rather than resources. It is clearly important to the OP based on his
wording that someone tell him that Array.Resize() won't free unmanaged
resources.
Not exactly. *Classes which implement IDisposable* should be Disposed.
Others should (and can) not. As far as the OP is concerned, resizing arrays
is exactly the same as any other operation that de-references any objects,
no more, and no less. Exiting a function that declares an instance of an
object does the same thing, since the scope of the object is the function,
which is placed on the stack and then removed from it. In other words, there
is nothing special about resizing arrays compared to other operations that
de-reference class instances.

The word "resources" is clearly defined. It refers to "A facility of a
computing system needed in order to perform an operation or task.Resources
include memory, storage, input/output units, processing units, data sets,
files, and programs." However, the most common resource used by any program
is memory, and the most common issue with regards to freeing up resources is
memory allocation/deallocation. The OP's question was specifically about
array reallocation, and the *vast* majority of classes in the CLR do not
implement IDisposable; therefore, they do not need to be disposed. The chief
purpose of Garbage Collection is memory management. Therefore, that is the
context in which I answered the question.
The OP spoke specifically of an array of objects, not structs. But in any
case when the array is resized, the old storage for the array must be
freed. The storage for the array proper is GC-tracked. For arrays of int
or any other value type, it's the only GC handle. It can be quite large
and it must be garbage collected.
Everything in .Net is an object. And I didn't mention anything about
structs. I did mention integers, and integers are objects. They are value
types, but they are objects nonetheless. In fact, a struct is an object,
because it is a value type.

At any rate, my first reply to you was in response to your assertion that my
first reply to the OP was incorrect, but in fact contained incorrect
information in itself, and would be therefore confusing to the OP:
Actually -- no. The objects will no longer be reachable through the
array. Only if there are no other references to those objects, will they
be garbage collected, and then only when you run low on memory.
The statement that Garbage Collection occurs only when memory is low is
false.
To free resources, you still have to call Dispose() by hand on each
object.
The blanket statement that "to free resources, you still have to call
Dispose() by hand on each object" is false as well. If this were the case,
all classes would implement IDisposable, and Garbage Collection would be of
no use. Since, as I mentioned, the vast majority of classes do not implement
IDisposable, the instances where Dispose must be called on de-referenced
classes are few and far between, and the subject of IDisposable is an
entirely different (and very specific) subject than the subject of whether
memory needs to be de-allocated by hand in the .Net Framework, as evidenced
by the OP's response to my answer,which was specifically about memory
allocation, from his previous experience as a C+ developer.

While one might helpfully point out the use of the IDisposable interface as
part of an answer to the question, the argument against my correct
information was both misleading and (as explained) contained false
information.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Kevin Spencer" <uc*@ftc.govwrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
>>Actually -- no. The objects will no longer be reachable through the
array. Only if there are no other references to those objects, will they
be garbage collected, and then only when you run low on memory. To free
resources, you still have to call Dispose() by hand on each object.

Incorrect. First, the question was, "are the resources allocated to the
objects that are now thown out of the array released properly by the
CLI?" The answer is, "yes." They are "released properly by the CLI."

There are 2 distinct errors in your answer. First, your assertion that
Garbage Collection only occurs when the system is low on memory. This is
not true. Low memory is only one of several criteria used to determine
when Garbage Collection occurs. Here is a good article that explains the
process:

http://www.csharphelp.com/archives2/archive297.html

A very nice discussion of what GC does when it runs, but I didn't see
anything about what triggers it. If you're running GC for any reason
other than low memory (i.e. calling GC.Collect()), something is wrong.
>>
The second errror is the statement "To free resources, you still have to
call Dispose() by hand on each object." First, most .Net classes do NOT
implement IDisposable, meaning, they do not HAVE a Dispose() method.
Apparently, they do not all need a Dispose() method. In fact, IDisposable
is an interface implemented for managed classes that use unmanaged
resources, or that use a lot of managed resources, and would benefit from
quick Garbage Collection. It is not implemented for most classes because
.Net Garbage Collection handles memory allocation quite well by itself in
most cases.

So yes, you admit that Dispose() is necessary for freeing any resources
other than memory (and often memory as well, if it's allocated outside the
CLR). Usually, the word resources is used to refer to exactly the things
Dispose() is needed for, if one meant "only memory" one would say memory
rather than resources. It is clearly important to the OP based on his
wording that someone tell him that Array.Resize() won't free unmanaged
resources.
>>
I have written large-scale applications that allocate dozens of megabytes
of RAM at a time, and they manage memory perfectly.

As for arrays, an array can be an array of anything. So, the idea that
creating a new instance of an array (which is what resizing does) is
going to necessitate Garbage Collection is simply mistaken. An array of
integers, for example, will not need any Garbage Collection, because
integers are value types. With an array of objects, yes, if there is a
reference to an object (in the array) in the running app, that object
will not be Garbage-Collected until that reference is removed. But on the
other hand, if you are still referencing an object, you don't WANT it to
be Garbage-Collected; you are still using it.

The OP spoke specifically of an array of objects, not structs. But in any
case when the array is resized, the old storage for the array must be
freed. The storage for the array proper is GC-tracked. For arrays of int
or any other value type, it's the only GC handle. It can be quite large
and it must be garbage collected.
>>
--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:eg**************@TK2MSFTNGP04.phx.gbl...
>>"Kevin Spencer" <uc*@ftc.govwrote in message
news:eJ**************@TK2MSFTNGP05.phx.gbl...
Yes.

Actually -- no. The objects will no longer be reachable through the
array. Only if there are no other references to those objects, will they
be garbage collected, and then only when you run low on memory. To free
resources, you still have to call Dispose() by hand on each object.

Sep 8 '06 #7
"Kevin Spencer" <uc*@ftc.govwrote in message
news:ef**************@TK2MSFTNGP04.phx.gbl...
>A very nice discussion of what GC does when it runs, but I didn't see
anything about what triggers it. If you're running GC for any reason
other than low memory (i.e. calling GC.Collect()), something is wrong.

You must have overlooked the following (from the article I referenced):
[snip]
>
While one might helpfully point out the use of the IDisposable interface
as part of an answer to the question, the argument against my correct
information was both misleading and (as explained) contained false
information.
This seems to have degenerated into a flame war, so I'm not going to address
your most recent set of mis-statements. I'll wait for the OP or someone
else to indicate interest in further clarification.
Sep 8 '06 #8

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

Similar topics

4
by: Christian Seberino | last post by:
How add a column to a 2d array/matrix in numarray??? The unelegant way I found was to: 1. Create a new array with an extra column (e.g. using 'zeros' function). 2. Copy original array into new...
6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
12
by: Maxwell2006 | last post by:
Hi, I declared an array like this: string scriptArgs = new string; Can I resize the array later in the code? Thank you, Max
3
by: John Devlon | last post by:
Hi Can anyony please tell me why I should use a List of objects instead of an array of objects ? Thanx John
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
9
by: JoeC | last post by:
I am crating a new version of my map game and my map will be a 2d array. I had problems trying to create a 2d array dynamically, in fact C++ won't let me do it. My question is how to create the...
8
by: Pim75 | last post by:
Hello, I'm defining a string array like: Dim strArray() As String = {"1", "2"} Can I add some values to this string array later in the code? It's not clear to me how to do this. I hope...
9
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've an optimization question for you all really quick. I have a stream that I am reading some bytes. At times, the stream can contain a small amount of bytes such as 50...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.