-
Sub-task
-
Resolution: Delivered
-
P3
-
10
-
Verified
Bytecode generation has been improved for enhanced for loops, providing an improvement in the translation approach for them. For example:
```
List<String> data = new ArrayList<>();
for (String b : data);
```
The following is the code generated after the enhancement:
```
{
/*synthetic*/ Iterator i$ = data.iterator();
for (; i$.hasNext(); ) {
String b = (String)i$.next();
}
b = null;
i$ = null;
}
```
Declaring the iterator variable outside of the for loop allows a null to be assigned to it as soon as it is no longer used. This makes it accessible to the GC, which can then get rid of the unused memory. Something similar is done for the case when the expression in the enhanced for loop is an array.
```
List<String> data = new ArrayList<>();
for (String b : data);
```
The following is the code generated after the enhancement:
```
{
/*synthetic*/ Iterator i$ = data.iterator();
for (; i$.hasNext(); ) {
String b = (String)i$.next();
}
b = null;
i$ = null;
}
```
Declaring the iterator variable outside of the for loop allows a null to be assigned to it as soon as it is no longer used. This makes it accessible to the GC, which can then get rid of the unused memory. Something similar is done for the case when the expression in the enhanced for loop is an array.