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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:55:55+00:00 2026-05-20T04:55:55+00:00

I know that Java’s implementation does everything it can to hide that information from

  • 0

I know that Java’s implementation does everything it can to hide that information from developers, but I’m building a memory-bound algorithm which does not depend on third-party libraries, so that information would come in handy.

In particular, I am allocating many large int[] arrays as instance variables. I will investigate more compact representation soon, but right now I’m interested in knowing how much space is used to a plain array.

  • 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-05-20T04:55:56+00:00Added an answer on May 20, 2026 at 4:55 am

    Well, an int field could end up taking up more than 4 bytes due to padding… but fundamentally it’s 4 bytes (32 bits). As for whether it does take more than 4 bytes, you’d have to experiment to find out. That’s reasonably easy to do for fields within an object; harder for stack-based int values.

    For example, a large array of int will always take up roughly 4 times its length (+ a small amount of overhead for the length etc). I don’t believe padding will be applied between elements – although it would be relatively easy to verify that. The value returned by Runtime.totalMemory() - Runtime.freeMemory() should only be used carefully, but if you allocate an array of 1 million ints in one run, then 2 million ints in another, then 3 million ints next etc it should be reasonably clear what’s going on.

    Sample code:

    public class Test {
      public static void main(String[] args) {
        int size = Integer.parseInt(args[0]);
    
        long before = getUsedMemory();
        int[] array = new int[size];
        long after = getUsedMemory();
    
        long difference = after - before;
        System.out.println("Total difference: " + (after - before));
        System.out.println("Per element: " + ((double) difference) / size);
        System.out.println(array); // Make sure the array isn't GC'd early
      }
    
      private static long getUsedMemory() {
        Runtime rt = Runtime.getRuntime();
        return rt.totalMemory() - rt.freeMemory();
      }
    }
    

    On my machine, trying this with 100000, 2000000 and 3000000 gives “4*size + 16” bytes for all runs.

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

Sidebar

Related Questions

i know that the JDK consists of all java packages .But what does the
I know that Open Office Database uses a java database backend. Does anyone have
I know that you can run almost all Java in Dalvik's VM that you
Does anyone know a good Java lib that will hook into SVN so I
Does anyone know of a Java library that handles finding sentence boundaries? I'm thinking
Does anyone know of any existing projects that aim to port Android's Java VM
I know that Java has beautiful inbuilt support for the HashMaps or HashTables. Does
I know that Java is pre-installed in Mac OS X but i wonder if
From J2me doc we know that: java.lang.InterruptedException Thrown when a thread is waiting, sleeping,
OK - I know that Java generics can be a minefield for the unwary,

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.