-2

It was an interview question that a List contains 5 objects like Organisation, Employee, Address,Country etc. How will you know which object is the heaviest one without running through java agent. There is one condition that all the objects available inside Arraylist are not serializable. Basically interviewer wants know how to write code know the size of the objects available inside ArrayList so that you can now that a particular object is heavier. Please provide me help. Once again let me put the conditions once again.

  1. You can not use any profiler tool.
  2. All the objects are not serializable.
  3. You can run though java agent.

You have to write code to test and run as normal java program.

  • 11
    That's an absolutely ridiculous question, not to mention that adding these 5 objects to the same list makes me want to hurl. – Sebastian Redl Aug 20 '13 at 16:01
  • 6
    http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object – Fernando Aug 20 '13 at 16:03
  • 11
    I'd start with "a list doesn't contain objects, it contains references". – Jon Skeet Aug 20 '13 at 16:04
  • @Jon Skeet he meant ArrayList I guess. – Malwaregeek Aug 20 '13 at 16:07
  • 1
    @Malwaregeek arrayList also contain references only. – Arpit Aug 20 '13 at 16:08
  • 1
    @Malwaregeek: what does that change? – Jeroen Vannevel Aug 20 '13 at 16:08
  • 1
    It worries me as a graduate going into the software development world how many horrible interview questions could be in my future. – Andrew Martin Aug 20 '13 at 16:10
  • @JonSkeet strictly speaking, a java.util.List does not contain references because that is an implementation and interfaces do not have implementation. I'd start with what is a List? what is an Object? and what is heavy? – emory Aug 20 '13 at 16:18
  • 3
    @emory: Well put it this way - a list *cannot* contain objects, simply because that's not how Java works. A particular list implementation can contain (e.g. indirectly via an array) references. When you're talking about the memory involved, it's really important to be clear about that difference. – Jon Skeet Aug 20 '13 at 16:20
  • @JonSkeet I agree, but since they are talking about the heaviness of objects and heaviness is not an intrinsic property of objects, I'll assume they are using a nonstandard definition of object which opens up the possibility that Lists can contain objects. Best to clarify basic terms before proceeding on this one. – emory Aug 20 '13 at 16:24
  • 1
    @emory: Right - that's my point. It's sloppy definition, and I *suspect* that the interviewer used the terminology sloppily to start with rather than it just being misrepresented. It feels like a bizarre question to me. – Jon Skeet Aug 20 '13 at 16:29

3 Answers3

1

You can use instrumentation interface. http://www.javapractices.com/topic/TopicAction.do?Id=83

Malwaregeek
  • 2,274
  • 3
  • 15
  • 18
1

You can't practically do this. Bear in mind that Java deals in references, and your list will simply contain a reference to the given object. Consider:

MyBigObject obj = new MyBigObject();

List<MyBigObject> list1 = new ArrayList<MyBigObject>();
list1.add(obj);

So your list contains a reference to your object. Now if I do this:

List<MyBigObject> list2 = new ArrayList<MyBigObject>();
list2.add(obj);

my second list contains a reference, to the same object. To say that list2 actually is the size of the 'contained' object is meaningless.

When you construct objects, they consist of primitives and references. You can account for the size of the primitives (since they're copied by value) but you can't do this for the references objects, since they're simply pointers to other objects. You can say an object is a certain size and made up of references (which may be 32 or 64 bit), but that's another matter.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

You can see how much space is needed to allocate an object by doing -XX:-UseTLAB on the command line and use this method

public static long memoryUsed() {
    return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}

long before = memoryUsed();
new Object();
long used = memoryUsed() - before; // 16 bytes.

You can also use reflection to scan through the fields of each object. You can use Unsafe to get the offset of each of the fields and estimate the end of the object (including object alignment)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130