Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda Expressions and Memory Management

How do the Lambda Expressions / Closures in C++0x complicate the memory management in C++? Why do some people say that closures have no place in languages with manual memory management? Is their claim valid and if yes, what are the reasons behind it?

like image 964
Surya Avatar asked May 22 '10 07:05

Surya


People also ask

Do lambda functions save memory?

Starting today, you can allocate up to 10 GB of memory to a Lambda function. This is more than a 3x increase compared to previous limits. Lambda allocates CPU and other resources linearly in proportion to the amount of memory configured. That means you can now have access to up to 6 vCPUs in each execution environment.

How much memory should I allocate to lambda?

You can configure the amount of memory allocated to a Lambda function, between 128 MB and 10,240 MB. The Lambda console defaults new functions to the smallest setting and many developers also choose 128 MB for their functions.

What is the main benefit of a lambda expression?

Fewer Lines of Code − One of the most benefits of a lambda expression is to reduce the amount of code. We know that lambda expressions can be used only with a functional interface. For instance, Runnable is a functional interface, so we can easily apply lambda expressions.

How does lambda expression improve performance?

Oracle claims that use of lambda expressions also improve the collection libraries making it easier to iterate through, filter, and extract data from a collection. In addition, new concurrency features improve performance in multicore environments.


2 Answers

Such arguments are a red herring. Yes, lambdas have memory management issues to deal with, but a lambda is basically like a function object (functor) with member variables. Whatever issues a functor has to deal with, a lambda has to deal with too. C++0x lambdas have facilities to decide which objects to capture and whether it should be by value or by reference. This is analogous to storing values and references in a functor object.

like image 123
Marcelo Cantos Avatar answered Oct 21 '22 11:10

Marcelo Cantos


Lambdas can outlive the context they were created in. Binding free variables by reference can be an issue then, because when the lambda wants to access them later, they may not exist anymore. It's simply "Don't return local variables by reference" in disguise.

like image 42
fredoverflow Avatar answered Oct 21 '22 09:10

fredoverflow