aaronburro Sup, B 53142 Posts user info edit post |
so yeah, I did some googling, and I've found various bits of info on how to get the actual size of an object in java. I know the size of the primitives,, but I'm looking for a more robust version which applies to freaking objects. I can't seem to find any actual methods that do it (as most sites say there isn't one,) but I did find a general guide as to how to jerry rig a measurement.
basically, its:
{ runtime.gc() long start = runtime.freememory()
instantiate the object and do your shit
//I found I need to call runtime.gc() again here, or else I get a crazy ass number long end = runtime.freememory()
sizeofobject = start - end
//$profit }
well, i did some inspecting and compared the base numbers for how the author described an Object: 4 words (16bytes). So, I test that theory. 8 bytes. A little bewildered, I try new String(), which I am told takes 60bytes. Nope, method returns 40bytes...
So, the question then is this: 1) am I doin my shit right, here, assuming that the only calls I am making between checking freememory() are either new Object() or new String()? 2) is there an easier fucking way that is actually reliable? 11/9/2005 12:51:58 AM |
philihp All American 8349 Posts user info edit post |
the JVM's memory manager is really a big black box. you're really not supposed to care. it's implementation-specific as to how big an object is... if there were some clever way of sharing memory between the objects, the JVM could do it as long as it maintains anything it is contractually obligated to do (such as remember what your variables are).
the JVM also doesn't have to run the garbage collector when you call .gc(). when you call it, you're really saying to the JVM, "now would be a convenient time to do it, if you want to do it now". but the JVM doesn't have to do it then, and it can really do it whenever the hell it wants.
but don't worry, deferring responsibility to the JVM to directly manage memory is generally regarded as a good thing. if you really want to go back to C, go ahead, just get very familiar with the words "Segmentation Fault". 11/9/2005 3:29:15 AM |
aaronburro Sup, B 53142 Posts user info edit post |
hehe. i know, right? I've got a programming assignment that wants us to maximize performance from the standpoint of runtime and memory usage, and I want to use the sizing information to tell me how much memory certain things are using... Specifically, I'm wanting to know if certain ideas I am testing are leading to a huge increase in memory as well as how much of an increase it actually contributes...
which do you think takes more memory? an int or an object reference? 11/9/2005 8:37:49 AM |
zorthage 1+1=5 17148 Posts user info edit post |
A way to get the size of an object would be to (as long as the object is serializeable), write it to a file and determine the size of that file. It might not be as percise as measuring the object in memory, but it will give you a consistent size of the object at different times.
I looked into this for work a while ago, and that was as close as I could get (was trying to determine how much data was being transfered across the network during an RMI call). 11/9/2005 8:11:51 PM |
bigben1024 All American 7167 Posts user info edit post |
They probably had caching and using less objects in mind. 11/9/2005 9:46:25 PM |
|