Created
January 26, 2018 14:05
-
-
Save mateusz-lisik/c8a4bc2bd4c4a5a8c32b208e3d1cce9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.lisik.experiments.cleanertest; | |
import java.lang.ref.Cleaner; | |
class MyCleaner implements Runnable { | |
private int currentInstance; | |
public MyCleaner(int currentInstance) { | |
this.currentInstance = currentInstance; | |
} | |
@Override | |
public void run() { | |
// final String someString = String.valueOf(currentInstance); | |
final String someString = "x"; | |
System.out.println("Instance " + someString + " is about to be destroyed"); | |
} | |
} | |
class SampleClass { | |
private static int instancesCount = 0; | |
private final int currentInstance; | |
SampleClass() { | |
currentInstance = instancesCount++; | |
System.out.println("Sample Class created, instance number: " + getCurrentInstance()); | |
Main.CLEANER.register(this, new MyCleaner(getCurrentInstance())); | |
} | |
public int getCurrentInstance() { | |
return currentInstance; | |
} | |
} | |
public class Main { | |
public final static Cleaner CLEANER = Cleaner.create(); | |
public static void main(String[] args) { | |
for (int i = 0; i <= 255; i++) { | |
new SampleClass(); | |
} | |
final byte[] bytes = new byte[999999999]; | |
System.gc(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment