0

Consider this snippet.

private String global_s;

public void onClick(View v) {
global_s = "String";

  new Thread(new Runnable() {
    public void run() {
        final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");     
        ImageView mImageView = (ImageView) findViewById(R.id.imageview);
        mImageView.setImageBitmap(bitmap);

        final String local_s = global_s;
        TextView tv = (TextView) findViewById(R.id.textview);
        tv.setText(local_s);
        });
    }
  }).start();
}

Will bitmap reference still alive (not collected by Garbage Collector) after the Thread ended ? since it's assigned on mImageView.

Also, how about local_s ? Will it reference be replaced with global_s reference ?

Aruman
  • 1,646
  • 1
  • 12
  • 12
  • Scope is a compile time concept that defines the use of names in a program. Reachability is a run time concept that describes what objects are retrievable through program execution. – Sotirios Delimanolis Oct 06 '15 at 20:47

2 Answers2

2

Both bitmap and local_s are local variables and therefore disposed as soon as run() ends (think milliseconds). However the Bitmap and the String objects will likely be stored as instance fields in the ImageView and TextView objects, sharing their lifespans (think seconds).

About the link between local_s and global_s: local_s captures the value of global_s at the time of assignment but is almost useless since it's then immediately passed to .setText() - note that Java is always pass by value, so another copy is immediately made and handed to .setText() that likely stores it in an instance field that is read at painting time to render the text.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
1

The GC will not destroy objects which are still reachable; i.e., so long as they have a reference, they will not be collected. If you have 5 minutes, skim this article, which provides an excellent, readable description of how Java garbage collections works.

Anyway, local_s just references the same object that global_s references. The local_s reference variable be destroyed as the thread's call stack is destroyed, but global_s still owns a handle to its String object on the heap. And since mImageView exists outside of the thread, its referenced objects will also not be collected once your local thread exists.

Keith
  • 3,079
  • 2
  • 17
  • 26
  • So that means bitmap reference not collected by GC after the Thread ended ? – Aruman Oct 06 '15 at 20:52
  • The _reference variables_ are destroyed. The _objects they reference_ are not, so long as something else references to them. See this [answer](http://stackoverflow.com/a/12429953/2356554) for a great explanation. – Keith Oct 06 '15 at 21:04
  • Sure -- check the SO answer in the comment I just edited -- it's pictures help :) – Keith Oct 06 '15 at 21:07
  • Call stacks don't return from threads. That makes no sense. Are you trying to say that the JVM may re-use the memory that was occupied by the call stack after the thread has been destroyed? – Solomon Slow Oct 06 '15 at 21:36