简体   繁体   中英

Java new Keyword internals

JVM is responsible for allocating heap memory for objects created using new keyword in Java based on size of the object. How does memory allocation work internally. Does JVM maintain a pointer to next big enough free block of memory and returns it or it delegates the responsibility of memory allocation to OS through system calls, like malloc in C internally calls brk() ?

It is JVM dependent and it is an implementation detail. But it is usually done via TLAB - thread local allocation buffer, faster then malloc , since it's a simple pointer bump. And that is a very-simplified explanation.

The canonical Java implementation uses a generational strategy for memory allocation.

https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/memman.html#wp1089132 http://www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf

Objects are allocated in "heap" memory, which is divided into two areas called "generations". There is a "young generation" area that's further divided into "Eden" (aka the "nursery") and two "survivor spaces". There is also an "old", or "tenured" generation area, comprising essentially all the heap not in the young generation.

Objects normally are allocated in Eden first. "Large" objects too big for Eden go into the tenured generation.

When Eden gets full, all the "live" objects, ones that are still reachable via some reference in the program, get copied into the first survivor space, then Eden is erased and the pointer to free Eden space is set to the beginning of the space.

Object allocation is simply a matter of designating the current "top" of Eden as the address of the new object, and the pointer to free Eden memory is increased by the size of the object. This takes something like 10 ns on modern computers. There is no equivalent to "free" because individual objects are never freed; the entire section of heap is simply erased and the free area pointer reset. When the first survivor space is full, then the JVM copies all the live objects from it and Eden into the other survivor space. Eden and the first survivor space are erased and their pointers reset. Now the other survivor space becomes "first", the old one "second", and the process repeats.

This is very speedy because dead objects are not tracked or marked, and well-written programs tend to have many relatively small objects of which most don't live long.

When objects do live a long time, they get "promoted" out of the young generation to the tenured generation, which is collected less frequently using slower algorithms. Tenured objects tend to die less quickly than in the young generation, and when they do, their space just becomes a "hole" of free space somewhere in the middle. Old-generation garbage collection typically involves moving live objects next to each other to consolidate blocks of free memory into larger blocks.

This is just a scratch of the surface. There's more to study.

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