Sitemap
Dec 28, 2024

```

public Runnable createRunnable(String taskName) {

String finalTaskName = new String(taskName); // Avoid capturing the outer variable

return () -> System.out.println("Executing: " + finalTaskName);

}

```

I didn't understand this optimization. Instead of the `taskName` variable, a new variable `finalTaskName` will remain in memory, so the overall amount of memory that will not be garbage collected still remains the same until the lambda is garbage collected. I could be wrong though.

No responses yet