Skip to content

Instantly share code, notes, and snippets.

@AntonKozlov
Created December 8, 2022 11:27
Show Gist options
  • Save AntonKozlov/e24754caa1a850d35d246faabaca845f to your computer and use it in GitHub Desktop.
Save AntonKozlov/e24754caa1a850d35d246faabaca845f to your computer and use it in GitHub Desktop.
import jdk.crac.*;
import java.util.HashSet;
import java.util.Set;
public class TestWeak {
public static void main(String[] args) throws Exception {
Context<Resource> context = new StrongContextWrapper(Core.getGlobalContext());
context.register(new Resource() {
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
System.out.println("beforeCheckpoint() invoked");
}
@Override
public void afterRestore(Context<? extends Resource> context) throws Exception {
System.out.println("afterCheckpoint() invoked");
}
});
//try force gc
for (int i = 0; i < 10; i++) {
System.gc();
}
Core.checkpointRestore();
}
}
class StrongContextWrapper extends Context<Resource> {
private Context<Resource> context;
private Set<Resource> strongLinks;
public StrongContextWrapper(Context<Resource> context) {
this.context = context;
this.strongLinks = new HashSet<>();
}
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws CheckpointException {
throw new CheckpointException("should not call this");
}
@Override
public void afterRestore(Context<? extends Resource> context) throws RestoreException {
throw new RestoreException("should not call this");
}
@Override
public void register(Resource resource) {
strongLinks.add(resource);
context.register(resource);
}
public void deregister(Resource resource) {
strongLinks.remove(resource);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment