How much memory is created on the heap if 'a' is defined?

W

Wenfei Ye

class A;
A a;

How much memory is created on the heap if 'a' is defined?

Thanks,

Wenfei
(e-mail address removed)
 
H

Hal Rosser

Wenfei Ye said:
class A;
A a;

How much memory is created on the heap if 'a' is defined?

Thanks,

Wenfei
(e-mail address removed)

memory is not created. its already there.
it may get allocated.
 
C

Chris Smith

Wenfei Ye said:
class A;
A a;

How much memory is created on the heap if 'a' is defined?

Using what virtual machine? And what code, since the code you've
written above doesn't compile? On a 32-bit platform, the declaration of
the variable 'a' would reserve 4 bytes of space, assuming that you have
validly defined a class called 'A' so that the code compiles. Those
four bytes would only be on the heap, though, if 'a' is an instance
field. If it's a local variable, they'll be on the stack, and if it's
static, they'll be in the static data area. Those 4 bytes are at least
theoretically there, but compiler optimization by either the bytecode or
JIT compilers might output any equivalent code that accomplishes the
same thing (so, for example, if you never refer to 'a', then one or the
other compiler may omit it). A 64-bit VM would reserve eight bytes
instead.

If you create an object of class A, then the answer is even more
dependent on the VM. Generally there is somewhere between 4 and 16
bytes of overhead for the object header, plus space for any data fields.
The class generally takes space for method vectors (sometimes called the
vtable) the method code itself (possible both the bytecode version and
one or more JIT-compiled versions), and other class description; and
this could be allocated from the same memory pool as the Java heap.
There is a Class object as well, which can either be created lazily by
the VM or instantiated proactively. There may be additional overhead on
either a per-class or per-object basis for garbage collection metadata,
which is part of the structure of the heap itself.

In the end, the answer is undefined. Why do you think that you need to
know?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top