• Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory allocation of Object Array  RSS feed

 
Ganni Kal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator

Thank you for your timely reply !

Regards
Ganesan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!