AutoreleasePool

  • @AutoreleasePool annotation triggers the translator to insert an autorelease pool around a method's body, ensuring temporary objects are deallocated.

  • It's applicable to void methods and helps manage temporary object allocation in high-level contexts or loops.

  • Using @AutoreleasePool on a loop variable creates a separate autorelease pool for each iteration, enhancing memory management within the loop.

  • This annotation is inherited from java.lang.annotation.Annotation and includes methods like annotationType(), equals(), hashCode(), and toString().

public abstract @interface AutoreleasePool implements Annotation

Annotation that indicates the translator should inject an autorelease pool around the method body. Only valid on methods that don't return anything.

Useful in high-level contexts to ensure that temporary objects allocated within the method or loop are deallocated.

Example usage:

 // Temporary objects allocated during execution of this method will
 // be deallocated upon returning from this method.
 @AutoreleasePool
 public void doWork() {
   ...
 }

 public void doWork(Iterable<Runnable> workToDo) {
   // Adding @AutoreleasePool on the loop variable causes a separate
   // autorelease pool to be attached to each loop iteration, clearing
   // up temporary objects after each iteration
   for (@AutoreleasePool Runnable item : workToDo) {
     item.run();
   }
 }
 

Inherited Method Summary