# Cleaner

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Nested Classes](https://developer.android.com/reference/java/lang/ref/Cleaner#nestedclasses)\|[Methods](https://developer.android.com/reference/java/lang/ref/Cleaner#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/java/lang/ref/Cleaner#inhmethods)  

# Cleaner

`
public

final

class
Cleaner
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|-----------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.lang.ref.Cleaner |

<br />

*** ** * ** ***

`Cleaner`manages a set of object references and corresponding cleaning actions.

Cleaning actions are[registered](https://developer.android.com/reference/java/lang/ref/Cleaner#register(java.lang.Object,%20java.lang.Runnable))to run after the cleaner is notified that the object has become phantom reachable. The cleaner uses[PhantomReference](https://developer.android.com/reference/java/lang/ref/PhantomReference)and[ReferenceQueue](https://developer.android.com/reference/java/lang/ref/ReferenceQueue)to be notified when the[reachability](https://developer.android.com/reference/java/lang/ref/package-summary#reachability)changes.

Each cleaner operates independently, managing the pending cleaning actions and handling threading and termination when the cleaner is no longer in use. Registering an object reference and corresponding cleaning action returns a[Cleanable](https://developer.android.com/reference/java/lang/ref/Cleaner.Cleanable). The most efficient use is to explicitly invoke the[clean](https://developer.android.com/reference/java/lang/ref/Cleaner.Cleanable#clean())method when the object is closed or no longer needed. The cleaning action is a[Runnable](https://developer.android.com/reference/java/lang/Runnable)to be invoked at most once when the object has become phantom reachable unless it has already been explicitly cleaned. Note that the cleaning action must not refer to the object being registered. If so, the object will not become phantom reachable and the cleaning action will not be invoked automatically.

The execution of the cleaning action is performed by a thread associated with the cleaner. The thread runs until all registered cleaning actions have completed and the cleaner itself is reclaimed by the garbage collector.

The behavior of cleaners during[System.exit](https://developer.android.com/reference/java/lang/System#exit(int))is implementation specific. No guarantees are made relating to whether cleaning actions are invoked or not.

Unless otherwise noted, passing a`null`argument to a constructor or method in this class will cause a[NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)to be thrown.

## Summary

|                                                                                     ### Nested classes                                                                                     ||
|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` interface` | [Cleaner.Cleanable](https://developer.android.com/reference/java/lang/ref/Cleaner.Cleanable) `Cleanable`represents an object and a cleaning action registered in a`Cleaner`. |

|                                                                                                                                                                                                                                    ### Public methods                                                                                                                                                                                                                                    ||
|-------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` static `[Cleaner](https://developer.android.com/reference/java/lang/ref/Cleaner)              | ` `[create](https://developer.android.com/reference/java/lang/ref/Cleaner#create(java.util.concurrent.ThreadFactory))`(`[ThreadFactory](https://developer.android.com/reference/java/util/concurrent/ThreadFactory)` threadFactory) ` Returns a new`Cleaner`using a`Thread`from the`ThreadFactory`.                                                                                     |
| ` static `[Cleaner](https://developer.android.com/reference/java/lang/ref/Cleaner)              | ` `[create](https://developer.android.com/reference/java/lang/ref/Cleaner#create())`() ` Returns a new`Cleaner`.                                                                                                                                                                                                                                                                        |
| ` `[Cleaner.Cleanable](https://developer.android.com/reference/java/lang/ref/Cleaner.Cleanable) | ` `[register](https://developer.android.com/reference/java/lang/ref/Cleaner#register(java.lang.Object,%20java.lang.Runnable))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj, `[Runnable](https://developer.android.com/reference/java/lang/Runnable)` action) ` Registers an object and a cleaning action to run when the object becomes phantom reachable. |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public methods

### create

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static Cleaner create (ThreadFactory threadFactory)
```

Returns a new`Cleaner`using a`Thread`from the`ThreadFactory`.

A thread from the thread factory's[newThread](https://developer.android.com/reference/java/util/concurrent/ThreadFactory#newThread(java.lang.Runnable))method is set to be a[daemon thread](https://developer.android.com/reference/java/lang/Thread#setDaemon(boolean))and started to process phantom reachable objects and invoke cleaning actions. On each call the[thread factory](https://developer.android.com/reference/java/util/concurrent/ThreadFactory#newThread(java.lang.Runnable))must provide a Thread that is suitable for performing the cleaning actions.

The cleaner terminates when it is phantom reachable and all of the registered cleaning actions are complete.

<br />

|                                                 Parameters                                                  ||
|-----------------|--------------------------------------------------------------------------------------------|
| `threadFactory` | `ThreadFactory`: a`ThreadFactory`to return a new`Thread`to process cleaning actions <br /> |

|                                             Returns                                             ||
|--------------------------------------------------------------------------|-----------------------|
| [Cleaner](https://developer.android.com/reference/java/lang/ref/Cleaner) | a new`Cleaner` <br /> |

|                                                                                                                     Throws                                                                                                                     ||
|--------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| [IllegalThreadStateException](https://developer.android.com/reference/java/lang/IllegalThreadStateException) | if the thread from the thread factory was[not a new thread](https://developer.android.com/reference/java/lang/Thread.State#NEW). |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException)                     | if the current thread is not allowed to create or start the thread.                                                              |

### create

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static Cleaner create ()
```

Returns a new`Cleaner`.

The cleaner creates a[daemon thread](https://developer.android.com/reference/java/lang/Thread#setDaemon(boolean))to process the phantom reachable objects and to invoke cleaning actions. The[context class loader](https://developer.android.com/reference/java/lang/Thread#getContextClassLoader())of the thread is set to the[system class loader](https://developer.android.com/reference/java/lang/ClassLoader#getSystemClassLoader()). The thread has no permissions, enforced only if a[SecurityManager is set](https://developer.android.com/reference/java/lang/System#setSecurityManager(java.lang.SecurityManager)).

All exceptions thrown by the cleaning actions in this thread are ignored.

The cleaner terminates when it is phantom reachable and all of the registered cleaning actions are complete.

<br />

|                                             Returns                                             ||
|--------------------------------------------------------------------------|-----------------------|
| [Cleaner](https://developer.android.com/reference/java/lang/ref/Cleaner) | a new`Cleaner` <br /> |

|                                                                            Throws                                                                             ||
|------------------------------------------------------------------------------------------|---------------------------------------------------------------------|
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException) | if the current thread is not allowed to create or start the thread. |

### register

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Cleaner.Cleanable register (Object obj, 
                Runnable action)
```

Registers an object and a cleaning action to run when the object becomes phantom reachable. Refer to the[API Note](https://developer.android.com/reference/java/lang/ref/Cleaner#compatible-cleaners)above for cautions about the behavior of cleaning actions.

<br />

|                                         Parameters                                          ||
|----------|-----------------------------------------------------------------------------------|
| `obj`    | `Object`: the object to monitor <br />                                            |
| `action` | `Runnable`: a`Runnable`to invoke when the object becomes phantom reachable <br /> |

|                                                          Returns                                                          ||
|----------------------------------------------------------------------------------------------|-----------------------------|
| [Cleaner.Cleanable](https://developer.android.com/reference/java/lang/ref/Cleaner.Cleanable) | a`Cleanable`instance <br /> |