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 9317337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:51:13+00:00 2026-06-19T02:51:13+00:00

The code at the bottom of this question is a bit long but basically

  • 0

The code at the bottom of this question is a bit long but basically creates a few objects and determines their size in memory. I execute the code with the following JVM parameters (TLAB to avoid chunk memory allocation and supposedly get accurate memory usage figures):

-server -Xms2000m -Xmx2000m -verbose:gc -XX:-UseTLAB

I run the code on a 64 bit Hotspot JVM and get the following output:

Java HotSpot(TM) 64-Bit Server VM
Object: 16 bytes

Object with 1 int: 16 bytes
Object with 2 ints: 24 bytes
Object with 3 ints: 24 bytes

Object with 1 long: 24 bytes
Object with 2 longs: 32 bytes
Object with 3 longs: 40 bytes

Object with 1 reference: 16 bytes
Object with 2 references: 24 bytes
Object with 3 references: 24 bytes

I conclude that:

  • an object takes 12 bytes, aligned to 16 bytes.
  • an int takes 4 bytes (1 object with one int is 12 + 4 = still 16 bytes, with 2 ints: 12 + 8 = 20 aligned to 24 bytes)
  • a long takes 8 bytes (1 object with one long is 12 + 8 = 20 bytes, aligned to 24 bytes)

But I struggle to understand why references don’t use as much space as longs.

Since references are 8 bytes on a 64-bit JVM, the obvious conclusion is that the measurement methodology has a flaw*.
Can you explain what is going on and what can be done to fix it?

*Notes:
– no GC runs during the measurement.
– using the Netbeans profiler yields similar results.

public class TestMemoryReference {

    private static final int SIZE = 100_000;
    private static Runnable r;
    private static Object o = new Object();
    private static Object o1 = new Object();
    private static Object o2 = new Object();
    private static Object o3 = new Object();

    public static class ObjectWith1Int  { int i; }
    public static class ObjectWith2Ints { int i, j; }
    public static class ObjectWith3Ints { int i, j, k; }
    public static class ObjectWith1Long  { long i; }
    public static class ObjectWith2Longs { long i, j; }
    public static class ObjectWith3Longs { long i, j, k; }
    public static class ObjectWith1Object  { Object o = o1; }
    public static class ObjectWith2Objects { Object o = o1; Object p = o2; }
    public static class ObjectWith3Objects { Object o = o1; Object p = o2; Object q = o3; }

    private static void test(Runnable r, String name, int numberOfObjects) {
        long mem = Runtime.getRuntime().freeMemory();
        r.run();
        System.out.println(name + ":" + (mem - Runtime.getRuntime().freeMemory()) / numberOfObjects + " bytes  ");
    }

    public static void main(String[] args) throws Exception {
        System.out.println(System.getProperty("java.vm.name") + "  ");

        r = new Runnable() { public void run() { for (int i = 0; i < SIZE; i++) o = new Object(); } };
        test(r, "Object", SIZE);

        r = new Runnable() { public void run() { for (int i = 0; i < SIZE; i++) o = new ObjectWith1Int(); } };
        test(r, "Object with 1 int", SIZE);

        r = new Runnable() { public void run() { for (int i = 0; i < SIZE; i++) o = new ObjectWith2Ints(); } };
        test(r, "Object with 2 ints", SIZE);

        r = new Runnable() { public void run() { for (int i = 0; i < SIZE; i++) o = new ObjectWith3Ints(); } };
        test(r, "Object with 3 ints", SIZE);

        r = new Runnable() { public void run() { for (int i = 0; i < SIZE; i++) o = new ObjectWith1Long(); } };
        test(r, "Object with 1 long", SIZE);

        r = new Runnable() { public void run() { for (int i = 0; i < SIZE; i++) o = new ObjectWith2Longs(); } };
        test(r, "Object with 2 longs", SIZE);

        r = new Runnable() { public void run() { for (int i = 0; i < SIZE; i++) o = new ObjectWith3Longs(); } };
        test(r, "Object with 3 longs", SIZE);

        r = new Runnable() { public void run() { for (int i = 0; i < SIZE; i++) o = new ObjectWith1Object(); } };
        test(r, "Object with 1 reference", SIZE);

        r = new Runnable() { public void run() { for (int i = 0; i < SIZE; i++) o = new ObjectWith2Objects(); } };
        test(r, "Object with 2 references", SIZE);

        r = new Runnable() { public void run() { for (int i = 0; i < SIZE; i++) o = new ObjectWith3Objects(); } };
        test(r, "Object with 3 references", SIZE);
    }
}
  • 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-19T02:51:15+00:00Added an answer on June 19, 2026 at 2:51 am

    Since references are 8 bytes on a 64-bit JVM

    This is your potentially-flawed assumption.

    HotSpot is able to use "compressed oops" to use 32-bit values for references in some places of the JVM (emphasis mine):

    Which oops are compressed?

    In an ILP32-mode JVM, or if the UseCompressedOops flag is turned off in LP64 mode, all oops are the native machine word size.

    If UseCompressedOops is true, the following oops in the heap will be compressed:

    • the klass field of every object
    • every oop instance field
    • every element of an oop array (objArray)

    I suspect this is what’s going on in your case.

    Test it by using

    -XX:-UseCompressedOops
    

    or

    -XX:+UseCompressedOops
    

    On my machine, by default I get the same results as you, but with -XX:-UseCompressedOops I see:

    Object:16 bytes
    Object with 1 int:24 bytes
    Object with 2 ints:24 bytes
    Object with 3 ints:32 bytes
    Object with 1 long:24 bytes
    Object with 2 longs:32 bytes
    Object with 3 longs:40 bytes
    Object with 1 reference:24 bytes
    Object with 2 references:32 bytes
    Object with 3 references:40 bytes
    

    … which is probably closer to what you were expecting 🙂

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

Sidebar

Related Questions

This is the code. I want to align text at bottom <p style=background:#eee;font-size:1.3em;color:#022662;height:116px> <img
This code should i place this code at bottom of body it's in conditional
EDITED SO THE CODE IS CORRECT (THANKS TO ta.speot.is) - NEW QUESTION AT BOTTOM
I have the following code but it doesnt seem to work: <td align=center style=padding-bottom:52.5px>
I have no idea why but only the bottom div works the code is:
I am trying to use a bit of code I found at the bottom
NOTE: This is a long question. I've explained all the 'basics' at the top
Ok, I know this seems like a duplicate question, but don't think it is.
my first question on Stackoverflow. Let me start with a bit of code. It's
This is a very simple (and probably dumb) question, but I can't seem to

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.