• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

which code is more efficient

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Out of these 2 piece of code which one will use less memory and why?Also what is the difference between these 2 piece of codes.

1.Contact [] ca = new Contact[10];
while(x<10)
{
ca[x]=new Contact();
x=x+1;
}

2.Contact ca ;
while(x<10)
{
ca = new Contact();
x=x+1;
}
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rakhee gupta:



Rakhi,
1. its ca is array of contacts. So it will consume more space.10 contact objects will be created & stored in array ca.
2.ca is single object so it will consume less space. Inside loop you are creating 10 contact objects but only 1 contact object will retain reference. All other contact objects are eligible for garbage collection

HTH
V
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean that in first case 10 active references to 10 objects will be there in the memory heap. but in 2nd case only 1 will be active at a time but then what about the other 9 contacts in the 2nd case will they be stored anywhere and can we access them any how?
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rakhee gupta:
In 2nd case only 1 will be active at a time but then what about the other 9 contacts in the 2nd case will they be stored anywhere and can we access them any how?



NOW you cant access them once you have lost reference to those 9 objects. You should have stored that reference inside the method somewhere , so that you can again access them

V
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you worrying about 10 object references' difference? The two bits of code are totally different in that one creates and retains 10 Contacts and that the other loses 9 of them. If you need 10 Contacts you need 10 Contacts. You are probably sitting in front of enough silicon to hold 1000000 Contacts easily.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Java is such a high-level language, it's hard to say for sure how much memory or CPU might be used on any given piece of code.

I can write a JVM (theoretically) that could break any assumptions you have about what occurs with your code.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bill Shirley:

I can write a JVM (theoretically) that could break any assumptions you have about what occurs with your code.



Well, yeah, but no. If your JVM made Vishal Matere's solution untrue, then it would not be a compliant VM.
 
reply
    Bookmark Topic Watch Topic
  • New Topic