
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of the Cleaner class in Java 9?
An Object that has been created during the program execution is automatically removed by Garbage Collector (GC). When an object not referenced by any thread and when JVM determines that this object can't be accessed, then it can be eligible for garbage collection.
The Object class has a finalize() method, which is automatically called by GC before it attempts to remove the object from the heap. In Java 9, the finalize() method has been deprecated and a new class java.lang.ref.Cleaner added to garbage collection management. An object of Cleaner class gets notified automatically when an object becomes eligible for garbage collection. An object that is being garbage collected needs to be registered with the cleaner object by using the register() method.
Example
import java.lang.ref.Cleaner; public class CleanerTest { public static void main(String args[]) { System.out.println("TutorialsPoint"); Cleaner cleaner = Cleaner.create(); if(true) { CleanerTest myObject = new CleanerTest(); cleaner.register(myObject, new State()); // register cleaner } for(int i = 1; i <= 10000; i++) { String[] largeObject = new String[1000]; try { Thread.sleep(1); } catch(InterruptedException e) { e.printStackTrace(); } } } private static class State implements Runnable { public void run() { System.out.print("Cleaning action"); } } }
Output
TutorialsPoint Cleaning action
- Related Questions & Answers
- What is the use of the Optional.stream() method in Java 9?
- What is the use of the toEpochSecond() method in Java 9?
- What is the use of the jdeprscan tool in Java 9?
- What is the use of underscore keyword in Java 9?
- What is the use of StrictMath class in Java?
- What is the use of the Tab key in JShell in Java 9?
- What are the changes of class loaders in Java 9?
- What is the use of a multi-version compatible jar in Java 9?
- What is the importance of REPL in Java 9?
- When to use the ServiceLoader class in a module in Java 9?
- What is the importance of the ProcessHandle interface in Java 9?
- What is the importance of the jcmd tool in Java 9?
- What is the super class of every class in Java?
- What is the purpose of using JLink in Java 9?
- What is the importance of jmod format in Java 9?