473,395 Members | 2,468 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,395 software developers and data experts.

Q. re object/string creation

Whats the difference between the following in terms of how the CLR
allocates and garbage collects? Which is better in terms of memory
usage/performance?

a)
foreach (string strURL in arrLongListOfURLs) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfURLs) {
strCopy = strURL;
}

And if the same logic was used with an HttpWebRequest
e.g.

httpWebRequest = (HttpWebRequest)WebRequest.Create (strURL);

is there anything extra to consider (apart from closing the response
and stream)?
Thanks
Nov 16 '05 #1
8 1223
Those two constructs results in *exactly* the same IL on my box
(.NET1.1). So there is no difference.

/Joakim

Lee Jackson wrote:
Whats the difference between the following in terms of how the CLR
allocates and garbage collects? Which is better in terms of memory
usage/performance?

a)
foreach (string strURL in arrLongListOfURLs) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfURLs) {
strCopy = strURL;
}

And if the same logic was used with an HttpWebRequest
e.g.

httpWebRequest = (HttpWebRequest)WebRequest.Create (strURL);

is there anything extra to consider (apart from closing the response
and stream)?
Thanks

Nov 16 '05 #2
Lee Jackson wrote:
Whats the difference between the following in terms of how the CLR
allocates and garbage collects? Which is better in terms of memory
usage/performance?

a)
foreach (string strURL in arrLongListOfURLs) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfURLs) {
strCopy = strURL;
}

And if the same logic was used with an HttpWebRequest
e.g.

httpWebRequest = (HttpWebRequest)WebRequest.Create (strURL);

is there anything extra to consider (apart from closing the response
and stream)?
Thanks


Hi,

as stated before, the exact same IL is produced for the two constructs.
It's up to the jitter to decide where the reference 'strCopy' will be
stored. As 'strCopy' is a local variable, it will be kept on the
thread's stack (most likely), or maybe in a register (less likely).
It's also up to the jitter to decide how many stack locations are
necessary throughout the execution of the method. Say you have 100
local variables, but only one of them is used at any point in time,
actually one stack location will be used (and reused). (I'm simplifying
things here to state my point). Or the jitter might decide to give
every local variable its own private location on the stack. No one
actually knows (without digging any deeper). Conclusion, you don't need
to worry about this.
So, assigning 'strUrl' to 'strCpy' declared inside or outside the loop
doesn't make any difference.

Cheers,
Benoit
Nov 16 '05 #3
On Sun, 09 Jan 2005 15:14:52 +0100, Benoit Vreuninckx
<bv*****@nospam.skynet.be> wrote:
Lee Jackson wrote:
Whats the difference between the following in terms of how the CLR
allocates and garbage collects? Which is better in terms of memory
usage/performance?

a)
foreach (string strURL in arrLongListOfURLs) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfURLs) {
strCopy = strURL;
}

And if the same logic was used with an HttpWebRequest
e.g.

httpWebRequest = (HttpWebRequest)WebRequest.Create (strURL);

is there anything extra to consider (apart from closing the response
and stream)?
Thanks


Hi,

as stated before, the exact same IL is produced for the two constructs.
It's up to the jitter to decide where the reference 'strCopy' will be
stored. As 'strCopy' is a local variable, it will be kept on the
thread's stack (most likely), or maybe in a register (less likely).
It's also up to the jitter to decide how many stack locations are
necessary throughout the execution of the method. Say you have 100
local variables, but only one of them is used at any point in time,
actually one stack location will be used (and reused). (I'm simplifying
things here to state my point). Or the jitter might decide to give
every local variable its own private location on the stack. No one
actually knows (without digging any deeper). Conclusion, you don't need
to worry about this.
So, assigning 'strUrl' to 'strCpy' declared inside or outside the loop
doesn't make any difference.

Cheers,
Benoit


Thanks for the (great) explanation :)

Still worried though ;)
Nov 16 '05 #4
Lee Jackson wrote:
On Sun, 09 Jan 2005 15:14:52 +0100, Benoit Vreuninckx
<bv*****@nospam.skynet.be> wrote:

Lee Jackson wrote:
Whats the difference between the following in terms of how the CLR
allocates and garbage collects? Which is better in terms of memory
usage/performance?

a)
foreach (string strURL in arrLongListOfURLs) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfURLs) {
strCopy = strURL;
}

And if the same logic was used with an HttpWebRequest
e.g.

httpWebRequest = (HttpWebRequest)WebRequest.Create (strURL);

is there anything extra to consider (apart from closing the response
and stream)?
Thanks


Hi,

as stated before, the exact same IL is produced for the two constructs.
It's up to the jitter to decide where the reference 'strCopy' will be
stored. As 'strCopy' is a local variable, it will be kept on the
thread's stack (most likely), or maybe in a register (less likely).
It's also up to the jitter to decide how many stack locations are
necessary throughout the execution of the method. Say you have 100
local variables, but only one of them is used at any point in time,
actually one stack location will be used (and reused). (I'm simplifying
things here to state my point). Or the jitter might decide to give
every local variable its own private location on the stack. No one
actually knows (without digging any deeper). Conclusion, you don't need
to worry about this.
So, assigning 'strUrl' to 'strCpy' declared inside or outside the loop
doesn't make any difference.

Cheers,
Benoit

Thanks for the (great) explanation :)

Still worried though ;)


Hi,

Worried about what, if I may ask ? The number of local variables used
throughout the method should be the least of your concerns. Stack space
does not have to be garbage collected, and it grows and shrinks
automatically when entering and leaving methods. It doesn't matter if
your method uses 10, 20 or even 100 local variables. Complexity with
regard to space and time are negligable.
Declaring the 'strUrl' inside or outside the loop doesn't have any
impact on how and when the objects pointed to are collected (it's the
same as the same IL is produced). So, is the object collected when you
assign another value to 'strUrl'? Nope. (Assuming the collector doesn't
kick in at that moment, and normally it won't). So only the reference
to the previous object is lost. BTW, it won't get collected as
'arrLongListOfURLs' still has a reference to it.
You shouldn't even bother that much about creating lots of small
objects, but of course, using less objects will always use less space
and time. The fastest application does not create any object, but it
won't do anything useful either ;) If you can do the same thing with
half the amount of objects, you should go that way.
Closing or disposing the webrequest (don't really know the class) before
assigning a new instance to the variable is a good practice.

regards,
Benoit
Nov 16 '05 #5
The memory issue is small as strings are reference types. The second is
probably better are it will re-use the same memory instead of asking for new
memoruy everytime.

Ciaran

"Lee Jackson" <le**@citymutual.com> wrote in message
news:u3********************************@4ax.com...
Whats the difference between the following in terms of how the CLR
allocates and garbage collects? Which is better in terms of memory
usage/performance?

a)
foreach (string strURL in arrLongListOfURLs) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfURLs) {
strCopy = strURL;
}

And if the same logic was used with an HttpWebRequest
e.g.

httpWebRequest = (HttpWebRequest)WebRequest.Create (strURL);

is there anything extra to consider (apart from closing the response
and stream)?
Thanks

Nov 16 '05 #6
I think you'll find that a string is a reference type and there the object
will be on the heap and a reference will be on the stack. Only objects
derived from System.ValueType actually live on the stack.

Ciaran
"Benoit Vreuninckx" <bv*****@nospam.skynet.be> wrote in message
news:41**********************@news.skynet.be...
Lee Jackson wrote:
On Sun, 09 Jan 2005 15:14:52 +0100, Benoit Vreuninckx
<bv*****@nospam.skynet.be> wrote:

Lee Jackson wrote:

Whats the difference between the following in terms of how the CLR
allocates and garbage collects? Which is better in terms of memory
usage/performance?

a)
foreach (string strURL in arrLongListOfURLs) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfURLs) {
strCopy = strURL;
}

And if the same logic was used with an HttpWebRequest e.g.

httpWebRequest = (HttpWebRequest)WebRequest.Create (strURL);

is there anything extra to consider (apart from closing the response
and stream)?
Thanks

Hi,

as stated before, the exact same IL is produced for the two constructs.
It's up to the jitter to decide where the reference 'strCopy' will be
stored. As 'strCopy' is a local variable, it will be kept on the
thread's stack (most likely), or maybe in a register (less likely). It's
also up to the jitter to decide how many stack locations are necessary
throughout the execution of the method. Say you have 100 local
variables, but only one of them is used at any point in time, actually
one stack location will be used (and reused). (I'm simplifying things
here to state my point). Or the jitter might decide to give every local
variable its own private location on the stack. No one actually knows
(without digging any deeper). Conclusion, you don't need to worry about
this.
So, assigning 'strUrl' to 'strCpy' declared inside or outside the loop
doesn't make any difference.

Cheers,
Benoit

Thanks for the (great) explanation :)

Still worried though ;)


Hi,

Worried about what, if I may ask ? The number of local variables used
throughout the method should be the least of your concerns. Stack space
does not have to be garbage collected, and it grows and shrinks
automatically when entering and leaving methods. It doesn't matter if
your method uses 10, 20 or even 100 local variables. Complexity with
regard to space and time are negligable.
Declaring the 'strUrl' inside or outside the loop doesn't have any impact
on how and when the objects pointed to are collected (it's the same as the
same IL is produced). So, is the object collected when you assign another
value to 'strUrl'? Nope. (Assuming the collector doesn't kick in at that
moment, and normally it won't). So only the reference to the previous
object is lost. BTW, it won't get collected as 'arrLongListOfURLs' still
has a reference to it.
You shouldn't even bother that much about creating lots of small objects,
but of course, using less objects will always use less space and time.
The fastest application does not create any object, but it won't do
anything useful either ;) If you can do the same thing with half the
amount of objects, you should go that way.
Closing or disposing the webrequest (don't really know the class) before
assigning a new instance to the variable is a good practice.

regards,
Benoit

Nov 16 '05 #7
Ciaran <ci****@theodonnells.plus.com> wrote:
The memory issue is small as strings are reference types. The second is
probably better are it will re-use the same memory instead of asking for new
memoruy everytime.


What would be "asking for new memory" every time? The two pieces of
code are identical as far as the IL is concerned, but the first is
cleaner in terms of limiting the scope of a variable to its use.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Ciaran <ci****@theodonnells.plus.com> wrote:
I think you'll find that a string is a reference type and there the object
will be on the heap and a reference will be on the stack. Only objects
derived from System.ValueType actually live on the stack.


No - the values of all local variables live on the stack. For reference
types, that means the references themselves rather than the objects,
but they still take up stack space.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

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

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
6
by: Busin | last post by:
Classes B, C, D and E are derived from base class A. A* CreateB(); A* CreateC(); A* CreateD(); A* CreateE(); class X { public:
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
6
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
3
by: Balaji Kannan | last post by:
Hi, In dot net during component development i have used some member variables in the class file. Inside the class i have used the member declaration and the instant handling in the following...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
2
by: nacho222 | last post by:
I'm currently in the middle of writing a persistence framework, and I have to make a design decission. The framework will take care of all the saving and restoring objects, and also the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.