Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Modern VM implementations reserve for each thread an own area on the heap to create objects in. So, no problem as long as this area does not get full (then the garbage collector moves the surviving objects).</p> <p>Further read: <a href="http://blogs.oracle.com/jonthecollector/entry/a_little_thread_privacy_please" rel="nofollow">how TLAB works</a> in Sun's JVM. <a href="http://www.azulsystems.com/blog/cliff/2009-12-22-touching-base" rel="nofollow">Azul's VM</a> uses slightly different approach (look at "A new thread &amp; stack layout"), the article shows quite a few tricks JVMs may perform behind the scenes to ensure nowadays Java speed.</p> <p>The main idea is keeping per thread (non-shared) area to allocate new objects, much like allocating on the stack with C/C++. The copy garbage collection is very quick to deallocate the short-lived objects, the few survivors, if any, are moved into different area. Thus, creating relatively small objects is very fast and <em><a href="http://en.wikipedia.org/wiki/Lock_free#Motivation" rel="nofollow">lock free</a></em>. </p> <p>The lock free allocation is very important, especially since the question regards <code>multithreaded environment</code>. It also allows true lock-free algorithms to exist. Even if an algorithm, itself, is a lock-free but allocation of new objects is synchronized, the entire algorithm is effectively synchronized and ultimately less scalable. <code>java.util.concurrent.ConcurrentLinkedQueue</code> which is based on the <a href="http://www.cs.rochester.edu/u/scott/papers/1996_PODC_queues.pdf" rel="nofollow">work of Maged M. Michael Michael L. Scott</a> is a classic example.</p> <hr> <p>What happens if an object is referenced by another thread? (due to discussion request)</p> <p>That object (call it <code>A</code>) will be moved to some "survivor" area. The survivor area is checked less often than the ThreadLocal areas. It contains, like the name suggests, objects whose references managed to escape, or in particular <code>A</code> managed to stay alive. The copy (move) part occurs during some "safe point" (safe point excludes properly JIT'd code), so the garbage collector is sure the object is not being referenced. The references to the object are updated, the necessary memories fences issued and the application (java code) is free to continue. Further read to this <a href="http://chaoticjava.com/posts/parallel-and-concurrent-garbage-collectors/" rel="nofollow">simplistic scenario</a>.</p> <p>To the very interested reader and if possible to chew it: the highly advanced <a href="http://usenix.org/events/vee05/full_papers/p46-click.pdf" rel="nofollow">Pauseless GC Algorithm</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload