0

I have been reading about garbage collector and its impact in the Android and I got some questions, that I would like to share.

  • What is the garbage collector's real impact in the Android ? It runs in own thread so the application shouldn't have any issue,right?
  • If the answer to question above was yes, should we create objects or avoid it? Example:

    findViewById(R.id.mytextview).setText("xpto");

or

TextView mytextView = findViewById(R.id.mytextview)
myTextView.setText("xpto");
Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
user1866731
  • 481
  • 2
  • 5
  • 13

2 Answers2

1

In you sample you don't create any objects (new operator), you create reference to existing Object in view hirarchy. Garbage collector looks through object graph and delete object no other objects reference to. There are few type of references. The most practically used Android feature is WeakReference, which help to avoid memory leaks (see What is the difference between a soft reference and a weak reference in Java?). You should be aware of referencing, if, say, you use timer and Activity may be destroyed while timer (or any long) task is still running( use weak reference)

Community
  • 1
  • 1
Alex Shutov
  • 3,217
  • 2
  • 13
  • 11
0

If you create objects in java, it's no bad. You shouldn't create objects in drawing cycles, like onDraw methods of Views. That depends on FPS - it can be not stable with frequently calls of Garbage Collector, so user will see lags. Although it could be re-creating Fragments\Activities on orientation change and so on

Petrov Dmitrii
  • 228
  • 1
  • 10