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

We know that we can use a concept Java Package, but I just wanted
I know that Java have its own garbage collection, but sometimes I want to
OK - I know that Java generics can be a minefield for the unwary,
I know that in Java you can specify the return type of the DoInBackground
I know that in GWT the java reflection tools isn't emulated, but is there
I know that Java code can call C++ code through JNI. Is it possible,
I know that I can set proxyhost and proxyport for the java application to
From the various online articles on Java 7 I have come to know that
I know that the Java can use the Socket Programming to send an Object.
We know that in java all classes extends Object. but Object itself is a

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.