2

Well i was working on optimizing my app,and i thought that each time we are setting up a listener,generally we do it like this

fadeOut.setAnimationListener(new AnimationListener() {
                            @Override
                            public void onAnimationEnd(Animation animation) {
                            }
                            @Override
                            public void onAnimationRepeat(Animation animation) {
                                }
                            @Override
                            public void onAnimationStart(Animation animation) {
                            }
                        });

and in this way,are we creating a new object everytime by not providing a variable name to it?i mean suppose I want to set the listener for fadeOut twice,won't it take twice the space for the two listeners that have been created ?shouldn't I create a variable instead ?and keep writing it over whenever needed?

will fadeOut.setAnimationListener(null) clear out all the variables that have been created ? Does this common practice takes so less memory that it can be ignored?

2 Answers2

1

Well, memory is not the only matter here. Of course, it will take twice as much memory as a single listener, but, most importantly, you are loosing the ability to get a cleaner and more meaning full code. Here is a better alternative :

//as a field
FadeAnimationListener fadeAnimationListener = new FadeAnimationListener();

//inside a method
fadeOut.setAnimationListener(fadeAnimationListener);

//as a static or not inner class or a public high level class
private static class FadeAnimationListener implements AnimationListener {
    @Override
    public void onAnimationEnd(Animation animation) {
        //do your specific stuff here
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        //do your specific stuff here
    }

    @Override
    public void onAnimationStart(Animation animation) {
        //do your specific stuff here
    }
 }

Note that in Eclipse, you could press CTRL+1 (mac : command + 1) and the quick fix menu will offer you to create a proper inner class from your anonymous inner class. So basically, this cleaner approach costs no key strokes/comes at no price.


About calling setListener(null), this is generally not needed as everything, from the component to its listener gets garbage collected together (when the activity dies), and the cost of a listener is not big enough to really care about it.

Also, read carefully the javadocs of the methods in that case, they may not all allow null listeners and react properly, though they usually do, but this is not a "well defined standard".

Snicolas
  • 36,854
  • 14
  • 106
  • 172
  • please please reply the last question in the above answer,it is important –  Apr 12 '14 at 21:52
  • You focus too much on garbage collection in that case. Memory leaks don't come from listeners in a java/android app. It's good to worry about this, but not here. – Snicolas Apr 12 '14 at 21:52
0

To answer your questions: Yes, a new object is being created every time you call that. But, the old object will be garbage collected because it won't be in use anymore. So there isn't much to worry about in terms of that.

fadeOut.setAnimationListener(null) will in fact clear out the currently assigned listener, but if you're trying to set a new listener then it doesn't make a difference because, like I said above, assigning a new listener will clear out the previous one from memory.

I would say that the more clean way to do it is to set it to 'this' and then implement AnimationListener inside your current class.

tambykojak
  • 2,029
  • 1
  • 11
  • 25
  • yes actually that was the part i was interested in:the garbage collector. –  Apr 12 '14 at 21:46
  • The garbage collector will examine all the objects in the heap at defined rates and then check to see if there is anything still pointing to that object. If not it will remove it. Of course it's a lot more complicated than that. – tambykojak Apr 12 '14 at 21:47
  • thank you for that...but are you 100% sure that garbage collector will do it's job instantly ? will it not depend on the frequency at which garbage collector will run –  Apr 12 '14 at 21:47
  • No, it won't do it instantly. The garbage collector runs at certain times in your application. Additionally, you might want to read about young and old generation memory: Here's an SO thread: http://stackoverflow.com/questions/2129044/java-heap-terminology-young-old-and-permanent-generations. – tambykojak Apr 12 '14 at 21:49
  • thank you...so if i want it to be instant...i should set the listener as null right?and then proceed to create a new listener ? –  Apr 12 '14 at 21:50
  • Yup, if you set the variable to null, then you will be doing it instantly without waiting for the garbage collector to run by and scoop it up. But it isn't truly needed in this case. – tambykojak Apr 12 '14 at 21:52
  • If you replace the listener by a new call to setListener, then, as no one will hold a reference to the previous listener, it will be garbage collected. That's a general principle in garbage collection in java. But really, here, you are splitting hairs in four where it's absolutely useless. – Snicolas Apr 12 '14 at 21:54
  • okay got it...but i don't want to be much dependent on garbage collector since i am getting memory leaks –  Apr 12 '14 at 21:55