Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trying to find the size of an object in Java  RSS feed

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,
I was trying to find the size of an object in Java , but the outputs are surprising.

Here is the code

---------------------------------------------------------------------------
class A
{
}


class Size
{
public static void main(String arg[])
{
Runtime r = Runtime.getRuntime();
long mem1=0 , mem2 =0 ;

System.out.println("Total Memory = " + r.totalMemory());


r.gc();
mem1=r.freeMemory();
System.out.println("Memory before Allocation = " + mem1);

//byte by[] = new byte[1000];
//int in[] = new int[1000];

//byte bc[] = new byte[4];
//byte bc[] = new byte[5];

//A z = new A(); // 72

r.gc();
mem2=r.freeMemory();

System.out.println("Memory after Allocation = " + mem2);
System.out.println("Memory Used = " + (mem1-mem2));
}

}
---------------------------------------------------------------------------
if i uncomment

//byte by[] = new byte[1000];

it gives me output 1016 (i think it is alloacting 1 byte for each byte variable and 16 byte is the size of reference of reference variable)


Similarly
//int in[] = new int[1000];
it gives me output 4016


But when i do

//byte bc[] = new byte[4];(Output 16)
//byte bc[] = new byte[5];(Output 24)

I can not understand , when i give the size of the array 5 it gives output as 24 but when size is 4 or less it gives 16.
(at least on my machine)



and if i instantiate z only and comment all above instantiation i get
A z = new A(); output as 72 .




Why am i getting output like this Please explain



Thanks in advance
[ February 18, 2006: Message edited by: faisal usmani ]
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many reasons, why the output of your test is not very useful... some of which are...

1. gc() command is not really any guarantee that it will run on all JVMs. At least, not to get you every byte back when completed.

2. freememory() is JVM specific -- related to OS specific page sizes.

3. Background threads may be running. The GC being one of them.

Henry
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sir ,

I got the point

regards
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that on some pratforms and some JVMs object always (or nearly always?) occupy a multiply of 8 bytes.

I recommend using more that one subsequent GC call, sometimes one is not enough.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to know the size of an object anyway?

People coming from a C or C++ background often think they need to know (because "sizeof" is widely used there), but really they don't. In Java, you can't get direct pointers to memory, anyway.

You may want to find the approximate size of an object, so as to be able to estimate how many such objects will fit in a Java heap of a given size. That's a fairly valid aim, I guess. You can estimate the size of an object by making lots of them, and measuring the change of freeMemory() after lots of gc(). You have to be very careful about it, though, as GC is very definitely not under your full control.

You can never get an exact answer. Also, the answer may vary between different implementations of the JVM, and different platforms.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!