简体   繁体   中英

Memory allocation : How much space does a reference occupy in Java?

In Java we have written a code:

A a1;
a1 = new A();

How many bytes of memory is reserved when compiler compiles the code:

A a1;

That's not specified by the Java standard and thus you should not worry about it.

Technically, references are usually as big as the machine's word size, ie 32 bit on a 32 bit machine and 64 bit on a 64 bit machine, though some 64 bit JVMs use special magic to allow 32 bit references.

One pointer's worth of memory is used on the stack. That should be 32 bits (4 bytes) unless your machine's in 64-bits mode.

edit:

I see that some people are confused and think that the A object itself is allocated on the stack. That is not the case in Java: all objects are allocated on the heap (modulo JIT optimizations of course). The line A a1; simply allocates pointer a1, initially set to NULL. The pointer itself is in the stack, though of course what it points to will be on the heap. The later call to new A() will allocate an A object on the heap, and the size of that allocation does depend on what's in A.

That depends on the platform and the implementation. For a 32-bit platform, a 4 byte pointer is used behind the scenes on object instances, regardless of the size of class A.

Edit:

The Java compiler does not reserve any memory for this, that's the runtime's (to be exact, the JIT's) responsibility.

A variable reference is a handle to an object on the heap, so it will take up a fixed amount (depending on the JVM implementation). However, just for that line, the compiler may not take up anything, since the variable has not been initialized yet. This is statically checked by the compiler, so it will know when it needs to allocate the variable and may in fact allocate it only when it is first assigned.

If you had a method:

 public static void method() {
    A a1;
 }

I would expect the compiler to optimize it out completely, as it can't do anything with it.

All that being said, in Java programming, you just don't worry about these things, they are determined by the JVM implementation and Java is not suitable for byte-level memory concerns. If you are counting bytes like that, you should be using C or some similarly close-to-the-metal language.

Was your question: How much space does a reference occupy in Java?

If that's the case I'm not sure, sorry.

A a1;

All the above does is define a local variable on the execution stack so no heap memory is reserved.

Enough to store a reference to any A! :-)

Note that it's generally impossible to know exactly how many bytes a particular implementation will actually use for a particular allocation, even in low-level languages like C: malloc() itself is a function which obviously needs to maintain internal data structures. To avoid fragmentation, it usually allocates a 2^n-sized block of memory. And so on.

If you're concerned about how much memory is actually used, write a sample program, and run it through your profiler.

如前所述,它将使用32位或64位,但是如果引用仅放置在寄存器中,则它可能不使用任何内存。

参考变量占用的字节数完全取决于jvm的结构(64位为8个字节,而32 bi为4个字节

A a1; allocates on the stack, not the heap.

However, this is all up to implementation, and is not actually defined, as far as I know.

即使对于堆栈中的内存量,这也将取决于A中包含/定义的内容。

null does not occupy any space in memory.

Simply saying int occupies some bytes like float occupies some space in memory.

But for null no space is occupied in memory. Send me details if my answer is wrong.

Try for system.memorrysize() like method in Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM