Memory Allocation Using Thread

I am wondering what happens if you declare a local thread in a method? Usually, all local variables will disappear as soon as the function returns, since they are all highlighted in Stack. However, it seems that the local thread will be different. It is right?

public int A() {
    Thread t = new Thread() {
        doSomething();
    }
    t.start();
    return -1;
 }
+3
source share
5 answers

A Thread is GC's own root. Therefore, at any time when you create a stream, despite its creation context, it will not be ready for the GC until its execution method is completed. This is true even if the local method is complete and the thread is still alive.

Example:

public void doSomeAsync(){
   Thread th = new Thread(new Runnable(){
      public void run(){
          Thread.sleep(500);
      }
   });
   th.start();
   //do something else quickly
}

//do somethign else quickly , , , GC. th GC .

+14

, , . , .

 public void startThread() {
      long var1 = 10;
      byte[] var2 = new byte[1024];
      final byte[] var3 = new byte[1024];
      final byte[] var4 = new byte[1024];
      Thread thread = new Thread(new Runnable() {
          private long var5 = 10;
          private byte[] var6 = new byte[1024];
          public void run() {
              int var7 = 100;
              byte[] var8 = new byte[1024];
              System.out.println("Size of var4 is " + var4.length);
              baz();
              ...
          }
          private void baz() {
              long var9 = 2;
              byte[] var10 = new byte[1024];
              ...
          }
      });
      thread.start();
 }

, , . Thread, Runnable .

  • thread. startThread(), Thread JVM. GC'd run(), Thread - JVM. Thread GC'd, , Thread, GC'd.
  • Runnable - - , . GC'd Thread GC'd.
  • var1 - startThread() . , startThread() , .
  • var2 - startThread() . , final. GC'd startThread().
  • var3 - startThread() . final, , . GC'd startThread().
  • var4 - startThread() . final . GC'd startThread(), Runnable Thread - GC'd.
  • var5 - Runnable Runnable. GC'd Runnable, Runnable Thread - GC'd.
  • var6 - Runnable . GC'd Runnable, Runnable Thread - GC'd.
  • var7 - run() . , run() , .
  • var8 - run() . GC'd run().
  • var9 - baz() . , baz() , .
  • var10 - baz() . GC'd baz().

:

  • , GC'd startThread() . Runnable GC'd.
  • final long varX, startThread() , , . startThread() , .
+7

, , Runnable run .

0

, , ​​ , Runnable ( - ) .

, , , . , ​​ , , Runnable (, , ).

0

Thread , , final, Thread. Thread run(), , , , , .

Only the variables finalused in the original method AND the generated thread method run()will refrain from garbage collection until both the method and the method are complete run(). If the thread does not gain access to the variable, then the presence of the thread will not prevent garbage collection after completion of the original method.

References

http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html

-1
source

All Articles