Search code examples
objectjvmallocation

Where does the JVM allocate objects


I would like to know wether a Java object is necessarily allocated in main memory or if the JVM can allocate an object in the processor cache also?


Solution

  • Objects are allocated in the heap. However reads and writes of memory will of course be cached in the processor. Different JVMs will do different things, but most will have a thread local allocation buffer which means that different threads will allocate objects in different partitions; the garbage collector will then recycle these and (if the object is still alive) move them to a different area of the heap memory as and when necessary.