?
Solved

How big is a bean, really?

Posted on 2007-08-03
11
Medium Priority
?
141 Views
Last Modified: 2013-11-05
Dear Experts,

I'm trying to figure out how much memory each bean actually takes up.  Of course, you need to count up the number of bytes you load into the fields it contains.  What I'm trying to figure is how much "overhead" there would be for the physical structure of the bean itself.

So assume I have something like the bean below.  It has two Integer and two String fields.  It has one non-standard method, which displays the values of the fields.  Leaving out the actual values I put in those fields, could anyone tell me how to calculate how much memory the bean itself would take up?

(For a few, it doesn't matter, but I'm sometimes creating a million at a time now, so bytes matter...)

Thanks!  Here's the bean:

public class BeanFTG_Routine_Type {
      private Integer rot_ProductCode;
      private Integer rot_RoutineCode;
      private String rot_RoutineDesc;
      private String rot_RoutineAlpha;

      public int hashCode() {
            int hash = 17;
            hash = hash * 37 + rot_ProductCode;
            hash = hash * 37 + rot_RoutineCode;
            return hash;
      }

      public boolean equals(Object obj) {
            if (this == obj) {
                  return true;
            }
            if ((obj == null) || (obj.getClass() != this.getClass())) {
                  return false;
            }
            BeanFTG_Routine_Type test = (BeanFTG_Routine_Type) obj;
            if (rot_ProductCode.longValue() != test.rot_ProductCode.longValue())
                  return false;
            if (rot_RoutineCode.longValue() != test.rot_RoutineCode.longValue())
                  return false;
            return true;
      }

      public void display(boolean includeNulls) {
            String s;
            s = "Bean values for BeanFTG_Routine_Type. includeNulls: "
                        + includeNulls;
            System.out.println(s);
            if ((includeNulls) || (rot_ProductCode != null)) {
                  s = "rot_ProductCode: " + rot_ProductCode + "@";
                  System.out.println(s);
            }
            if ((includeNulls) || (rot_RoutineCode != null)) {
                  s = "rot_RoutineCode: " + rot_RoutineCode + "@";
                  System.out.println(s);
            }
            if ((includeNulls) || (rot_RoutineDesc != null)) {
                  s = "rot_RoutineDesc: " + rot_RoutineDesc + "@";
                  System.out.println(s);
            }
            if ((includeNulls) || (rot_RoutineAlpha != null)) {
                  s = "rot_RoutineAlpha: " + rot_RoutineAlpha + "@";
                  System.out.println(s);
            }
      }

      public Integer getRot_ProductCode() {
            return rot_ProductCode;
      }

      public Integer getRot_RoutineCode() {
            return rot_RoutineCode;
      }

      public String getRot_RoutineDesc() {
            return rot_RoutineDesc;
      }

      public String getRot_RoutineAlpha() {
            return rot_RoutineAlpha;
      }

      public void setRot_ProductCode(Integer rot_ProductCode) {
            this.rot_ProductCode = rot_ProductCode;
      }

      public void setRot_RoutineCode(Integer rot_RoutineCode) {
            this.rot_RoutineCode = rot_RoutineCode;
      }

      public void setRot_RoutineDesc(String rot_RoutineDesc) {
            this.rot_RoutineDesc = rot_RoutineDesc;
      }

      public void setRot_RoutineAlpha(String rot_RoutineAlpha) {
            this.rot_RoutineAlpha = rot_RoutineAlpha;
      }
}
0
Comment
Question by:BrianMc1958
  • 6
  • 3
  • 2
11 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 19629948
There will surely be some exact information about this at SUN's but I did some experiments.
What you are looking for is the basic size of an Object.
I found 10 bytes overhead for every Object created.
Probably an address, a size and 2 bytes for typing purposes?

;JOOP!
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 2000 total points
ID: 19629969
Then again, this might be platform dependent.

This article suggests that the Object administration only takes 8 bytes:
http://www.javaworld.com/javaworld/javatips/jw-javatip130.html

But you can do some research yourself based on this article:
http://www.javaworld.com/javaworld/javaqa/2003-12/02-qa-1226-sizeof.html

I would say: many articles come down to a basic Bean size of 10.
I would not worry too much about the memory used, your data is certainly much more.

;JOOP!
0
 
LVL 14

Expert Comment

by:CPColin
ID: 19640190
A Macguyver-ish way of calculating this would be to run the garbage collector, store the current amount of available memory, create a bunch of objects, run the garbage collector again, and compute the difference in available memory, then divide that by how many objects you created. You'd be using the System.gc() and Runtime.freeMemory() methods to do it.

It probably wouldn't be very accurate, since the VM would be messing around with memory management behind the scenes, but you could probably get close to an accurate value by running the test a few times and taking the average.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 24

Expert Comment

by:sciuriware
ID: 19640331
CPColin, you just CAN NOT run the garbage collector, you can only give it a push.
The running of the GC is completely in the 'hands' of the JVM.

In my experiments I therefore measured just before and after instantiating those Objects
so I had nothing to do with the Garbage Collector.

;JOOP!
0
 
LVL 14

Expert Comment

by:CPColin
ID: 19640417
From the Javadoc: "When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects." I agree that it's, in general, poor form to run the garbage collector explicitly, but I figured it'd help make the test results a little more accurate by, hopefully, avoiding a garbage collection in the middle of creating all the objects.

But yeah, you might as well not invoke the garbage collector, and instead just make a few extra test runs so the average is more accurate.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 19641378
No, not at all.
The only proper way is to start with a clean application and then to instantiate Objects.
The means that:
- no Objects have to be returned,
- no unused heap is re-used,
- all heap growth is assigned to the created Objects.

Any other approach spoils the party.
By the way, the Object architecture is NOT a single list of used and unused memory;
in fact it is a hybrid system of lists of various kinds om memory in so many states.
That's why it is impossible to use the heap size on an application that has Objects re-used or returned.

;JOOP!
0
 

Author Comment

by:BrianMc1958
ID: 19645347
Yikes.  No fighting in front of the newbies, please!

I understand there would be a tiny overhead to just store the address of the bean.  But what about the METHODS in the bean?  Is there additional memory PER BEAN allocated for that?  Now that I'm thinking about it (instead of just asking Experts Exchange), it seems dumb to think so.  There would only be one copy of the LOGIC stored, right?  So you can have all the code you want.  It's just the (instance) variables that are allocated on a per-bean basis, right?
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 2000 total points
ID: 19645410
Of course not, methods are PER CLASS.
Thus the bean has an address of 4 bytes pointing to a Class object that has pointers and
type records (signatures) to all methods that are defined for the class.
Remember: 10000000000 objects instantiated from a class have ONE set of methods!!!

;JOOP!
0
 

Author Comment

by:BrianMc1958
ID: 19645619
OK!  OK!  I'm just coming around to see the beauty of OO from another perspective.

Thanks for the 10000000000000th time,
BrianMc1958
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 19645699
And do not worry too much about those few bytes.
35 years ago we had to be thrifty.

;JOOP!
0
 

Author Comment

by:BrianMc1958
ID: 19645996
The most recent going rate I heard was nine cents per gig.  That's a lot of objects.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question