Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Java8 lambdas maintain a reference to their enclosing instance like anonymous classes?

We know that anonymous classes maintain a reference to their enclosing instance and that this can lead to context leaks on Android.

Since retrolambda backports lambdas to Java7, it could be worth a try.

It seems that Java8 lambdas do not have this problem, but I can't find any official information on that.

Any clue?

like image 843
Renaud Cerrato Avatar asked Feb 11 '15 04:02

Renaud Cerrato


People also ask

Are lambdas anonymous classes?

A lambda expression is a short form for writing an anonymous class. By using a lambda expression, we can declare methods without any name. Whereas, Anonymous class is an inner class without a name, which means that we can declare and instantiate class at the same time.

Can lambda expressions be used in place of instances of anonymous classes?

By the way, you cannot always use lambda expression in place of Anonymous class, because of its limitation of being SAM type. If you are using an anonymous class to implement an interface with two abstract methods then you cannot replace it with a lambda of Java 8.

Does lambda expression eliminates the need of anonymous class?

Lambda expressions are introduced in Java 8. These are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.

Is lambda a anonymous function Java?

Simply, lambda is an anonymous function which can be passed around in a concise way. Or, An anonymous function (function literal, lambda abstraction abstraction, lambda function, lambda expressionor block) is function definition that is not bound to an identifier.


2 Answers

Lambda expressions and method references capture a reference to this only if required, i.e. when this is referenced directly or an instance (non-static) member is accessed.

Of course, if your lambda expression captures the value of a local variable and that value contains a reference to this it implies referencing this as well…

like image 193
Holger Avatar answered Oct 01 '22 16:10

Holger


Here is some info.

From the following link http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html: This has a beneficial implication for memory management: while inner class instances always hold a strong reference to their enclosing instance, lambdas that do not capture members from the enclosing instance do not hold a reference to it. This characteristic of inner class instances can often be a source of memory leaks (the so-called lapsed listener problem)

You can also see http://docs.oracle.com/javase/tutorial/java/javaOO/whentouse.html from the text: Nested class: Use it if your requirements are similar to those of a local class, you want to make the type more widely available, and you don't require access to local variables or method parameters.

Use a non-static nested class (or inner class) if you require access to an enclosing instance's non-public fields and methods. Use a static nested class if you don't require this access.

like image 31
Hamed Moghaddam Avatar answered Oct 01 '22 15:10

Hamed Moghaddam