• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Memory allocation of Object Array

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

There is a big difference in the size memory allocated by the following two statements

Statement 1:

Object[] obj1 = new Object[0]; // takes memory, say 250 bytes

Statement 2:

Object[] obj2 = new Object[100000]; // takes memory around 10000 bytes

My question is, even though the memory will be allocated only after we create the objects and add to the array, why the second statements is occupying around 10000 bytes in memory?

Your help will be appreciated!!!

Thanks
Ganni
 
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
Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name here. Thanks!

Finally, the number in your example is not 10,000, but 100,000. An object reference (even a null one) is a 32-bit integer in the most common JVM implementation, so the empty array actually takes 4 bytes per element, or 400,000 bytes -- plus a little bit for for object overhead.
 
Ganni Kal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you for your timely reply !

Regards
Ganesan
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic