473,770 Members | 5,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 arrLongListOfUR Ls) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfUR Ls) {
strCopy = strURL;
}

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

httpWebRequest = (HttpWebRequest )WebRequest.Cre ate (strURL);

is there anything extra to consider (apart from closing the response
and stream)?
Thanks
Nov 16 '05 #1
8 1249
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 arrLongListOfUR Ls) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfUR Ls) {
strCopy = strURL;
}

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

httpWebRequest = (HttpWebRequest )WebRequest.Cre ate (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 arrLongListOfUR Ls) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfUR Ls) {
strCopy = strURL;
}

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

httpWebRequest = (HttpWebRequest )WebRequest.Cre ate (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 arrLongListOfUR Ls) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfUR Ls) {
strCopy = strURL;
}

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

httpWebRequest = (HttpWebRequest )WebRequest.Cre ate (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 arrLongListOfUR Ls) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfUR Ls) {
strCopy = strURL;
}

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

httpWebReque st = (HttpWebRequest )WebRequest.Cre ate (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
'arrLongListOfU RLs' 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**@citymutua l.com> wrote in message
news:u3******** *************** *********@4ax.c om...
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 arrLongListOfUR Ls) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfUR Ls) {
strCopy = strURL;
}

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

httpWebRequest = (HttpWebRequest )WebRequest.Cre ate (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.ValueTyp e 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 arrLongListOfUR Ls) {
string strCopy = strURL;
}
b)
string strCopy;
foreach (string strURL in arrLongListOfUR Ls) {
strCopy = strURL;
}

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

httpWebRequ est = (HttpWebRequest )WebRequest.Cre ate (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 'arrLongListOfU RLs' 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****@theodon nells.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Ciaran <ci****@theodon nells.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.ValueTyp e 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.co m>
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
5654
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 data. The class is contains these private variables:
6
2299
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
2979
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 Intel compiler it has stopped working. I think the singleton pattern static instance thing may be at the root of the problem, but I'm not sure. I had to add calls to instance() in regCreateFn, which gets the behaviour more in line with what I was...
54
4612
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " + salaryMinLabel.value); } I also have an asp.net object:
6
2342
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 there a shortcut for creating such array ? eg //simple array indexed via numbers var a = new Array("a", "fdsf", "sada"); or,
3
1388
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 way. In the constructor i have created the connection instant in the following way
8
3452
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 COM objects - or, just use the interactive user If they choose to use the interactive option, the impersonate="true" means that the process runs under the interactiv user (which I've confirmed works correctly). If they specify a userid/password, I...
15
1926
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 two? (1)
12
5556
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. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
2
1851
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 relationships between them. Obviously, I need a way to identify objects uniquely. Now, one way would be to use an autogenerated id from a autoincremental field in the DB. But the problem is that I need to know the ID of the object as soon as it is created,...
0
9591
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9425
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
10225
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...
1
7415
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
5312
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
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.