Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7885679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:08:00+00:00 2026-06-03T05:08:00+00:00

I would like to know exactly the real space allocated in memory for an

  • 0

I would like to know exactly the real space allocated in memory for an object.

I try to explain with some example: using a 64 bit JVM, pointer size should be 8 bytes, so:

  • Object singletest = new Object(); will take 8 bytes to reference the Object plus the size of the Object
  • Object arraytest = new Object[10]; will take 8 byte to reference the position where the array is stored plus 8*10 bytes to store the array plus the size of each Object
  • int singleint = new int; will take just 2 bytes, because int is a primitive type
  • int[] arrayint = new int[10]; will take 8 bytes to reference the position and 10*2 bytes for the elements

Moreover, this is the reason why Java allows to write code like this:

int[][] doublearray = new int[2][];
int[0][] = new int[5];
int[1][] = new int[10];

What really happen is that an array will produce a reference (aka pointer) like an object, so it doesn’t really matter the size of the second dimension at declaration time (and dimensions can be different, there is no link between them). Then the space taken will be: a reference to doublearray (8 bytes), first dimension is simply a reference to the second one, so other 8 bytes * 2 (first dimension size), and finally 2 bytes * 5 plus 2 bytes * 10.

So, finally, if have a real class like this:

class Test {
   int a, b;
   int getA() {return A};
   void setA(int a) {this.a = a;}
   int getB() {return B};
   void setB(int b) {this.b = b;}
}

when I call a new to instantiate, a pointer (or name it reference, because it’s Java) of 8 bytes will be used plus 2+2bytes to store the integers into the class.

The questions are: am I right or I wrote total nonsense? Moreover, when I don’t instantiate an object but I just declare it, 8 bytes will be allocated for further use or not? And what if I assign a null value?

Meanwhile for primitive type I’m quite sure that just declaring it will allocate the requested space (if I declare an “int i” then I can immediately call i++ because no reference are used, just a portion of memory is setted on “0”).

I searched on internet without clever response… I know that I wrote a lot of questions, but any help will be appreciated! (and maybe I’m not the only one interested)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:08:01+00:00Added an answer on June 3, 2026 at 5:08 am

    using a 64 bit JVM, pointer size should be 8 bytes,

    Actually its usually 32-bit unless you have a maximum heap size of 32 GB or more heap. This is because Java uses references, not pointers (and each object is on an 8 byte, not 1 boundary)

    The JVM can change the size of a reference depending on which JVM you use and what the maximum heap size is.

    Object singletest = new Object(); will take 8 bytes to reference the Object plus the size of the Object

    The object will use about 16 bytes of heap. It may or may not use 4 bytes of stack, but it could just use a register.

    Object arraytest = new Object[10];

    This will use about 16 bytes for the header, plus 10 times the reference sizes (about 56 bytes in total)

    int singleint = new int; will take just 2 bytes, because int is a primitive type

    int is always 32-bit bit, you can’t create a newprimitive. As its notionally on the stack it might use 4-bytes of stack or it might only use a register.

    int[] arrayint = new int[10]; will take 8 bytes to reference the position and 10*2 bytes for the elements

    Again the object is likely to be the same size as the new Object[10] (56 bytes)

    int[][] doublearray = new int[2][];
    int[0][] = new int[5];
    int[1][] = new int[10];
    

    I wouldn’t call it a doublearray as it could be confused with double[]

    However the size is likkely to be about 16 + 2 * 4 for doublearray and 16 + 5*4 + 4 (for padding) and 16 + 10 * 4.

    Memory allocated on the heap is aligned to an 8 byte boundary.

    I call a new to instantiate, a pointer (or name it reference, because it’s Java) of 8 bytes will be used plus 2+2bytes to store the integers into the class.

    The reference is on the stack and usually this is not included.

    The object has a header of about 16 bytes and the int values take up 2 * 4 bytes.

    when I don’t instantiate an object but I just declare it

    Java doesn’t let you declare Objects, only primitives and references.

    what if I assign a null value?

    That could change the value of the reference, but otherwise nothing happens.

    if I declare an “int i” then I can immediately call i++ because no reference are used, just a portion of memory is setted on “0”

    No heap will be used, possibly no stack will be used (possibly 4 bytes). Possible the JIT compiler will remove the code if it doesn’t do anything.

    maybe I’m not the only one interested

    … but was not too afraid to ask. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following issue and I would like to know what exactly happens.
I would like to know what scripts for LaTeX mean and what they exactly
i would like know some reference. I know i can googling it. but prefer
I would like to know what guys are using to make diagram of your
I would like to know exactly what is going on with omp parallel for
I would like to debug and profile some 64-bit software performing unaligned accesses, like
I would like to know how exactly model binding works in ASP.NET MVC3. Since
I would like to know exactly how the Is command works in Linux and
Would like to know what a programmer should know to become a good at
Would like to know the c# code to actually retrieve the IP type: Static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.