简体   繁体   中英

Do create View passing Activity as Context cause memory leak?

public class MainActivity extends Activity {
    private TextView textView;
    protected void onCreate(Bundle savedInstanceState) {
        textView = new TextView(this);
        textView.setText("some text");
        setContentView(textView);
    }
}

We have two objects: one instance of MainActivity and one instance of TextView and both of them have reference to each other. So basically it will cause the memory leak. Am I right?

It is a question related to garbage collection algorithms

What you are thinking is Reference counting algorithm . As you are saying, it has some problem with the circular reference situation. So it is not used in modern JVMs anymore.

You should check out other garbage collection algorithms and see how modern JVM gc works.

https://www.dynatrace.com/resources/ebooks/javabook/how-garbage-collection-works/

不,请参阅答案

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM