Re: Vector<T> length in bytes
Andrea Francia wrote:
ivan.kataitsev@gmail.com wrote:
=) Mark, thank u for your help, but in context of my task i need to
know exactly the "length" in bytes, not memory allocation of byte[].
After some actions all data will be send to the some output stream anf
to make correct blocks of data i need to know the subj. Anyway, thanx
for ur help.
If you wanna know the serialized size of the object you can serialize
the object in a ByteArrayOutputStream and the call the size() method.
http://java.sun.com/javase/6/docs/api/java/io/ByteArrayOutputStream.html
This, of course, is not related to the memory it occupies in the JVM.
In general there is no way to know how much memory a Vector occupies. Do you
count the objects stored in the Vector? (And why Vector? That class has been
all but obsolete for ten years.) Do you count items referenced from those
objects? What of those indirectly referenced objects also live in other
structures or collections? Do you count the memory towards their usage as well?
What if the JIT compiler enregisters or puts some of that on the stack? What
if it inlines away some of the references altogether?
Don't forget to include the footprint of the monitors attached to the Vector
and each of its elements.
In short, there is no 100% reliably accurate way to measure the size of a
Vector as you describe the problem.
--
Lew