473,320 Members | 1,922 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,320 software developers and data experts.

Another memory question

Going back a week or so I posted a question about whether it was better to
declare an object within a for loop so it gets declared each time or out of
the loop and then reuse the object each time.
Basic consensus was that it was better to declare it outside if it was going
to take a while to initialise.

Anyway..

More thoughts
If we declare it within the loop then each time it will create a new object
on the heap as compared to declaring it outside and setting it to new each
time which would theoretically reuse the object already on the heap.
Eventually to my thinking this will degrade performance as the GC will be
forced into action to clean up the heap.

On second thougths I dont think this is correct.
If we set any object to new it will create a new object on the heap
regardless of where the object was declared?
If we are setting an object to an existing object then it will simply point
to that object on the heap regardless of where it was declared?

Any thoughts??

Cheers

JB
Life is not a reality TV show.
Nov 16 '05 #1
15 1813

"Erik Frey" <er*******@hotmail.com> wrote in message
news:OO**************@TK2MSFTNGP10.phx.gbl...
You have your object on the heap, and a reference to it on the stack.
Whether you declare a reference to the object inside or outside your loop
actually has no effect on the resultant IL. You can check this out yourself using ildasm.

Thought so.

Thanks Erik.
JB
Nov 16 '05 #2
> Any thoughts??

Cheers

JB
Life is not a reality TV show.


Barring arcane wizadry that I'm not aware of, every time you call new you
put an object on the heap.

You have your object on the heap, and a reference to it on the stack.
Whether you declare a reference to the object inside or outside your loop
actually has no effect on the resultant IL. You can check this out yourself
using ildasm.

Erik
Nov 16 '05 #3

"Erik Frey" <er*******@hotmail.com> wrote in message
news:OO**************@TK2MSFTNGP10.phx.gbl...
You have your object on the heap, and a reference to it on the stack.
Whether you declare a reference to the object inside or outside your loop
actually has no effect on the resultant IL. You can check this out yourself using ildasm.

Thought so.

Thanks Erik.
JB
Nov 16 '05 #4
John Baro <jo***@NOSPAMmesware.com.au> wrote:
Going back a week or so I posted a question about whether it was better to
declare an object within a for loop so it gets declared each time or out of
the loop and then reuse the object each time.
Basic consensus was that it was better to declare it outside if it was going
to take a while to initialise.


I don't believe that was the consensus. I believe the consensus was
that it would be better to declare it outside if (and only if) you
could reuse the same *object* (not just the same variable) for all
iterations of the loop - in other words, if you don't need to recreate
it for each iteration.

I believe that answers the rest of your question.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Erik Frey <er*******@hotmail.com> wrote:
Barring arcane wizadry that I'm not aware of, every time you call new you
put an object on the heap.


There is *one* piece of arcane wizardry that I'm aware of (and am
slightly horrified by):

using System;

class Test
{
static void Main()
{
object x = new string(new char[]{});
object y = new string(new char[]{});

Console.WriteLine (x==y);
}
}

In other words, creating "new" empty strings actually doesn't (in some
cases, at least) - it reuses String.Empty.

Not that it's really relevant to the OP's question, but I thought you
might be interested...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
John Baro <jo***@NOSPAMmesware.com.au> wrote:
Going back a week or so I posted a question about whether it was better to
declare an object within a for loop so it gets declared each time or out of
the loop and then reuse the object each time.
Basic consensus was that it was better to declare it outside if it was going
to take a while to initialise.


I don't believe that was the consensus. I believe the consensus was
that it would be better to declare it outside if (and only if) you
could reuse the same *object* (not just the same variable) for all
iterations of the loop - in other words, if you don't need to recreate
it for each iteration.

I believe that answers the rest of your question.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Erik Frey <er*******@hotmail.com> wrote:
Barring arcane wizadry that I'm not aware of, every time you call new you
put an object on the heap.


There is *one* piece of arcane wizardry that I'm aware of (and am
slightly horrified by):

using System;

class Test
{
static void Main()
{
object x = new string(new char[]{});
object y = new string(new char[]{});

Console.WriteLine (x==y);
}
}

In other words, creating "new" empty strings actually doesn't (in some
cases, at least) - it reuses String.Empty.

Not that it's really relevant to the OP's question, but I thought you
might be interested...

--
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
> In other words, creating "new" empty strings actually doesn't (in some
cases, at least) - it reuses String.Empty.

Not that it's really relevant to the OP's question, but I thought you
might be interested...


Ah, hm. Interesting - I guess they use some selective overrides like
System.ValueType does.

I am going to whip this tidbit out at parties ;)

Thanks Jon.

Erik
Nov 16 '05 #9
Erik Frey <er*******@hotmail.com> wrote:
Not that it's really relevant to the OP's question, but I thought you
might be interested...
Ah, hm. Interesting - I guess they use some selective overrides like
System.ValueType does.


What do you mean, exactly? I don't think it's really an override in the
normal sense (where ValueType actually overrides Equals, GetHashCode,
and ToString for instance). There's nothing *to* override in String. I
think it's just a grotty hack somewhere. Some time I should have a look
in the Rotor code and see if I can work out whether that does it too...
I am going to whip this tidbit out at parties ;)


Now there's a sentence which could be taken out of context :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
> What do you mean, exactly? I don't think it's really an override in the
normal sense (where ValueType actually overrides Equals, GetHashCode,
and ToString for instance). There's nothing *to* override in String. I
think it's just a grotty hack somewhere. Some time I should have a look
in the Rotor code and see if I can work out whether that does it too...


I have NO idea about .NET internals, but I always assumed that
System.ValueType did something in its constructor (it has its own protected
constructor) that actually held some kind of specification (or IL) for how
to be allocated, and System.String might do something similar.

That might be a bit of a simplistic view, though.

Erik
Nov 16 '05 #11
Erik Frey <er*******@hotmail.com> wrote:
What do you mean, exactly? I don't think it's really an override in the
normal sense (where ValueType actually overrides Equals, GetHashCode,
and ToString for instance). There's nothing *to* override in String. I
think it's just a grotty hack somewhere. Some time I should have a look
in the Rotor code and see if I can work out whether that does it too...


I have NO idea about .NET internals, but I always assumed that
System.ValueType did something in its constructor (it has its own protected
constructor) that actually held some kind of specification (or IL) for how
to be allocated, and System.String might do something similar.

That might be a bit of a simplistic view, though.


I don't think it really works like that. Value types are odd - they're
sort of two types in one: one which is a reference type, which is the
boxed type - and that derives from ValueType - and one which is the
value type itself, and which you can't really get at except in normal
value type operation, if you see what I mean. It's all a bit weird
though, to be honest. Seems to work, mind you :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12
> In other words, creating "new" empty strings actually doesn't (in some
cases, at least) - it reuses String.Empty.

Not that it's really relevant to the OP's question, but I thought you
might be interested...


Ah, hm. Interesting - I guess they use some selective overrides like
System.ValueType does.

I am going to whip this tidbit out at parties ;)

Thanks Jon.

Erik
Nov 16 '05 #13
Erik Frey <er*******@hotmail.com> wrote:
Not that it's really relevant to the OP's question, but I thought you
might be interested...
Ah, hm. Interesting - I guess they use some selective overrides like
System.ValueType does.


What do you mean, exactly? I don't think it's really an override in the
normal sense (where ValueType actually overrides Equals, GetHashCode,
and ToString for instance). There's nothing *to* override in String. I
think it's just a grotty hack somewhere. Some time I should have a look
in the Rotor code and see if I can work out whether that does it too...
I am going to whip this tidbit out at parties ;)


Now there's a sentence which could be taken out of context :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #14
> What do you mean, exactly? I don't think it's really an override in the
normal sense (where ValueType actually overrides Equals, GetHashCode,
and ToString for instance). There's nothing *to* override in String. I
think it's just a grotty hack somewhere. Some time I should have a look
in the Rotor code and see if I can work out whether that does it too...


I have NO idea about .NET internals, but I always assumed that
System.ValueType did something in its constructor (it has its own protected
constructor) that actually held some kind of specification (or IL) for how
to be allocated, and System.String might do something similar.

That might be a bit of a simplistic view, though.

Erik
Nov 16 '05 #15
Erik Frey <er*******@hotmail.com> wrote:
What do you mean, exactly? I don't think it's really an override in the
normal sense (where ValueType actually overrides Equals, GetHashCode,
and ToString for instance). There's nothing *to* override in String. I
think it's just a grotty hack somewhere. Some time I should have a look
in the Rotor code and see if I can work out whether that does it too...


I have NO idea about .NET internals, but I always assumed that
System.ValueType did something in its constructor (it has its own protected
constructor) that actually held some kind of specification (or IL) for how
to be allocated, and System.String might do something similar.

That might be a bit of a simplistic view, though.


I don't think it really works like that. Value types are odd - they're
sort of two types in one: one which is a reference type, which is the
boxed type - and that derives from ValueType - and one which is the
value type itself, and which you can't really get at except in normal
value type operation, if you see what I mean. It's all a bit weird
though, to be honest. Seems to work, mind you :)

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

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

Similar topics

26
by: Materialised | last post by:
Hi everyone, I seen the post by Rob Morris, and thought that I would double check that I was using pointers in the correct way. So I written the following string functions to test. I know soem can...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
10
by: Steven Blair | last post by:
Hi, Quick overview of the problem: public bool Something( out DataSet ds ) { bool ret=false; try {
8
by: GeekBoy | last post by:
I understand the benefit of pushing the StateServer process onto another computer to "balance" the load and take some cpu and memory usage off the web server, but how much could it possibly help?...
39
by: anonymous | last post by:
Dear All, >From my understanding of pointers, a pointer should not be able to access a memory location until that memory has been allocated either by assiging the address of a variable or...
5
by: Yourko | last post by:
Hi there! I`me currently trying to write some simple programs in C. For one such program i created globals.h file. In that file i defined a structure of type _client, and a pointer of that type: ...
19
by: lawtrevor | last post by:
I have a question regarding the use of free() based on some code I need to decipher: { struct A {<A fields>}; struct B {<A fields+ <Bfields>}; typedef struct A Aobj; typedef struct B Bobj;
11
by: Sensei | last post by:
Hi again! I have ``yet another silly question'', about arrays this time. I've looked through the FAQs in the array & memory sections without finding an answer. I surely didn't look deep enough. ...
14
by: rohitkumar | last post by:
this is my code to calculate the sum of digits in a number for eg. 432567 sum =4+3+2+5+6+7=27 i 'm using turboc++ compiler from borland and i 'm runing my program on DOS. ...
11
by: michelqa | last post by:
Hello, I can retrieve column text from a ListView in another process but I cant figure out how to access to structure elements (LVCOLUMN) <code> //Handle variable is a valid ListView handle ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.