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

  • Home
  • SEARCH
  • 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 7895953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:46:07+00:00 2026-06-03T07:46:07+00:00

Possible Duplicate: In Java, what is the best way to determine the size of

  • 0

Possible Duplicate:
In Java, what is the best way to determine the size of an object?

If we create instance variables and instance methods inside a class then how to find out the total memory allocated for each Object of this class, which as shown as below eg, i think it is some of variables (a,b) size and method area size(display). is it true? what about main method area which is written inside a class.

for example:

class A {  
    int a;
    float b;
    void display()
    {  
       ---
    }

    public static void main(String a[])
    { 
        A obj=new A();
    }
}
  • 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-03T07:46:09+00:00Added an answer on June 3, 2026 at 7:46 am

    The short answer is to you don’t need to know unless you know its a problem.

    The longer answer is that it will use about 16 bytes of the header of the object, 4 bytes for each field (and rounding the total to an 8 byte boundary) i.e. 24 bytes.

    local variables e.g. references are allocated on the stack and usually are not counted.

    The reason you don’t usually bother with the size is that 1 MB is worth about 2 seconds of your time (comparing retail price for memory with the minimum wage) This means the time it takes you to ask the question is worth far more than the value of the memory you could possibly save (in this case)

    [A 4 GB module costs £18 and minimum wage is £8.50 so 1 GB is worth about 0.5 hours. 1 MB is worth about 2 seconds.]

    Put another way, if a simple change doesn’t save you 1 MB of memory you have a better use for, you could be wasting your time.

    The same applies for mobile devices but to a lesser degree (memory costs more and if its deployed to thousands of devices the total cost of saving a few hundred bytes can matter)

    A consequence of this is that the time it takes you to write long instead of int or double instead of float (They both have one extra character) can be worth thousands of times more than the extra memory it uses. However I would still suggest almost always using double instead of float and often long instead of int, because if it saves one bug it will more than make up for the extra characters you had to type.


    The simplest way to measure how much space an object uses (or how much memory is used to create an object) is to turn the TLAB off -XX:-UseTLAB and measure the amount of memory before and after the object is created with Runtime.totalMemory – freeMemory.

    However, I prefer to wait until a data structure shows up in a profiler like YourKit (VisualVM is free) before I consider it a problem.

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

Sidebar

Related Questions

Possible Duplicate: In Java, what is the best way to determine the size of
Possible Duplicates: In Java, what is the best way to determine the size of
Possible Duplicates: sizeof java object In Java, what is the best way to determine
Possible Duplicate: What is the best way to convert a java object to xml
Possible Duplicate: Best way to iterate through a directory in java? I want to
Possible Duplicate: Best way to really grok Java for a C# guy I worked
Possible Duplicate: Create Excel file in Java How to save output in excel format
Possible Duplicate: What’s the best way to implement an ‘enum’ in Python? What is
Possible Duplicate: Best Java obfuscator ? Well, I'm planning on releasing a Jar into
Possible Duplicate: What is the best open-source java charting library? (other than jfreechart) In

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.