Well, a byte array is an object, so... if this object that is referred to is a byte array, then you can simply cast it back to a byte array reference before you use it.
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)
Serialising will give you a byte array, providing the object is Serializable. However, the length of this byte array will not be the same as the size of the object in memory; the serialised version is usually bigger.
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.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I think in this case also you are seralizing the object obj as only the serializable objects can be written to the ObjectOutputStream.
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.
Joined: Oct 30, 2001
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.
Joined: Jan 17, 2007
Thanks for answers:
You help me to understand that not a way for obtain the size for an object.
Thanks Peter, and other people.
I found another solution about this issue.
But I thought that it was possible.
Thanks a lot.
Joined: Oct 18, 2004
Well .... What you are talking about is the second part of the question.
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.