Java nonblocking memory allocation

后端 未结 3 746
野趣味
野趣味 2021-02-01 09:00

I read somewhere that java can allocate memory for objects in about 12 machine instructions. It\'s quite impressive for me. As far as I understand one of tricks JVM using is pre

3条回答
  •  被撕碎了的回忆
    2021-02-01 09:47

    The best trick is the generational garbage-collector. This keeps the heap unfragmented, so allocating memory is increasing the pointer to the free space and returning the old value. If memory runs out, the garbage-collection copy objects and creates this way a new unfragmented heap.

    As different threads have to synchronize over the pointer to the free memory, if increasing it, they preallocate chunks. So a thread can allocate new memory, without the lock.

    All of this is explained in more detail here: http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html

提交回复
热议问题