Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are lambdas garbage collected?

If I'm not mistaken, under certain situations a lambda in Java is generated as an anonymous class instance. For example, in this code the lambda needs to capture a variable from the outside:

final int local = 123456;
list.forEach(x -> System.out.println(x + local));

Does it means that the garbage collector will claim the lambda as an object?

like image 671
JavaJavaDingDingDing Avatar asked Nov 30 '14 11:11

JavaJavaDingDingDing


People also ask

Are lambdas faster than for loops Java?

The answer is it depends. I have seen cases where using a lambda was slower and where it was faster. I have also seen that with newer updates you get more optimal code.

What is the point of lambdas?

Lambda functions are intended as a shorthand for defining functions that can come in handy to write concise code without wasting multiple lines defining a function. They are also known as anonymous functions, since they do not have a name unless assigned one.

What is garbage collection in AWS?

Garbage collection and indicators for your application's health. The purpose of JVM garbage collection is to clean up objects in the JVM heap, which is the space for an application's objects. It finds objects which are unreachable and deletes them. This frees heap space for other objects.

Are lambdas immutable?

Lambda expressions help us achieve pure functions, immutability, and first-class functions principles in Java. Lambda functions are pure because they do not rely on a specific class scope. They are immutable because they reference the passed parameter but do not modify the parameter's value to reach their result.


1 Answers

Yes, instances that represent lambda expressions are garbage collected just like any other object. Even the classes that implement the functional interface can be collected, if the class that contains the lambda expression gets eligible for collection.

The reason that the above lambda is not collected is the result of an optimization by the lambda bootstrap method. Normally, a bootstrap method binds a static factory method to the invokedynamic call site. That factory takes all required variables of the enclosing methods and a reference to the declaring instance, if required. The factory then creates an instance of the class that represents the lambda and stores all values in fields of this instance.

However, if a lambda expression does not have any state, the bootstrap method creates a singleton which is then directly bound to the lambda expression's call site. Naturally, this singleton cannot be collected before the enclosing class is.

If you want to read more about how invokedynamic and lambda expressions work, I recently wrote a summary: http://mydailyjava.blogspot.no/2015/03/dismantling-invokedynamic.html?m=1

This is the current implementation of HotSpot, the bootstrapper can change in the future.

like image 83
Rafael Winterhalter Avatar answered Oct 05 '22 23:10

Rafael Winterhalter