However, if this object is not a byte array, then it would depend on what this object is. Is there a method that will return the underlying byte array? Or is there a meaning to converting to a byte array? etc.
Originally posted by Nacho Espinosa:
I have an Array Object (Bean), I want to get the size for this array object, I think that obtaining the byte, I get the size, when I use the length attribute for this object.
The length field of the array will return the number of elements in the array. With Java, there is no way to determine the physical byte size of an object. (ie. there is no sizeof operator)
There is no proper way to obtain the size of a Java object in memory. But there are very few legitimate reasons to need to know it. Often, people who think they need to know the size are people coming from a C/C++ background, who are used to dealing with the raw memory in a way that Java programs just don't do.
If you really do need to know the size, reasonable estimates can be made. Search the forums. This has been discussed many times before.
I agree with Peter Chase , we cannot find the in memory size of the object.moreover how a jvm interprets the bytecode and makes objects in the heap is completely vendor specific and there is no exposed api's to interrogate this stuff.
Originally posted by Pradeep Kadambar:
Ok ... Eat this ready to eat stuff. This is cooked with ObjectOutputStream.
Although it's not 100% clear, I don't think the original poster wanted to serialise his object (well, an array, actually). He wanted to know the true size in memory of the object. As I and another have explained, this is not possible in Java - only an estimate can be made.
If serialisation were wanted, then the posted code basically works, but has several poor features.
The only one I'll mention is that the squashing of IOException and returning empty array could mask a programming error. No real I/O is happening - only in-memory pseudo-I/O - and hence an IOException should never happen. If an IOException did somehow happen, the last thing you'd want to do is squash it, because it would mean something seriously wrong.
Although it's not 100% clear, I don't think the original poster wanted to serialise his object (well, an array, actually). He wanted to know the true size in memory of the object.
That's exactly why your are not totally clear about what was asked. As far as the solution it's not about giving fool proof solutions. If just has to give the idea.
All in good sense