• Post Reply Bookmark Topic Watch Topic
  • New Topic

Will there be any memory difference in Heap delare object in these two styles?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a question in mind that
Suppose this is my code


will there be any memory or perfomance difference in these two approach?


Thanks
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do YOU think?
Have you done any experiments?

Which do you think will create more objects?
Is object creation expensive?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What do YOU think?
Have you done any experiments?

Which do you think will create more objects?
Is object creation expensive?



that'e why I have posted question
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you need to ShowSomeEffort or at least prove that you have done SOME thinking about this issue.

What is your best guess at this point?
It doesn't necessarily have to be right, but it most probably will be.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there should be memory difference
because each time I am calling new Myclass() a new object will be created in Heap

And In first case I am just creating a reference to a object and then later using it in my program
So I guess not much memory will take in this case


this is just my guess
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now was that so hard?
Yes, the second example will create more objects

If it creates more objects and uses more memory, then it needs to do Garbage collection more often. That might be a performance issue in the long run.

It depends on how expensive these objects are.

If each MyClass instantiation takes 10 seconds and consumes 500MB of memory then yes you WILL see a big difference between the two examples.
If it takes <0.1s and consumes <1kb of memory then you probably wouldn't notice the difference.

You might want to look up Premature Optimization.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks stevan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!