munHunger
munHunger

Reputation: 2999

lambda adding to class count

So rather quick question:

If I have a program that is using a lot of lambda expressions, is each lambda creating a new class that is stored in metaspace?

Normally it would be fine if it is stored there, but what if you also add the java flag -Xnoclassgc, would that create a leak that eventually crashes the program?

Upvotes: 1

Views: 244

Answers (2)

Yahya
Yahya

Reputation: 14092

Adding -Xnoclassgc flag doesn't disable the whole Garbage Collector. It only disables the Garbage Collector of classes from PermGen.

However, in JDK8, we no longer have the PermGen because the metadata has now moved to native memory to an area known as the Metaspace.

On the other hand, Lambda expressions in Java are instances of Functional Interfaces (a functional interface is an interface that contains exactly one abstract method).

In other words, lambdas are objects and treated like other objects and stored on the heap, so compiler does not create .class file for every instance of lambda.

Upvotes: 1

neta
neta

Reputation: 61

The short answer is no.

Under the hood they use an instruction introduced in Java 7 - invokedynamic

You can watch this presentation for more information - https://www.infoq.com/presentations/lambda-invokedynamic

Upvotes: 0

Related Questions