Reputation: 992
I am trying understand about the size that a Java object will be allocated with when created using a new operator.
Consider that i am creating a class
public class NewClass {
NewClass() { }
}
when i create an instance of NewClass
using NewClass nc = new NewClass();
. what is the size of the NewClass
that gets created in the heap?
~ Jegan
Upvotes: 1
Views: 3171
Reputation: 20110
Profiling is the best way, but you can get a good estimate like so:
8 bytes per object (bare overhead), plus fields.
The final result is increased to the nearest multiple of 8 bytes.
See also my example here for how to calculate memory use on a more complex object. Note: these rules may vary with VMs, and may change as newer versions of the VM come out. My estimate only applies to the Sun JVM, although I suspect IBM's results will be similar.
Upvotes: 9
Reputation: 20906
I think you need to use a profiler to measure this. You may use JProfiler or YourKit profilers for this.
Upvotes: 0