Skip to content

Commit

Permalink
Additional teardown to aid GC and release resources
Browse files Browse the repository at this point in the history
Most of this would happen as part of GC clearing the runtime, but
these changes help reduce that load and get the large collections
and data structures releasable earlier.

Fixes jruby#8343
  • Loading branch information
headius committed Jan 15, 2025
1 parent 01d88c3 commit b43d1dc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
15 changes: 13 additions & 2 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
Expand Up @@ -3402,6 +3402,18 @@ private void systemTeardown(final ThreadContext context) {
threadService.teardown();
threadService = new ThreadService(this);

// Release classloader resources
releaseClassLoader();

// Tear down LoadService
loadService.tearDown();

// Clear runtime tables to aid GC
boundMethods.clear();
allModules.clear();
constantNameInvalidators.clear();
symbolTable.clear();
javaSupport = loadJavaSupport();
}

private int userTeardown(ThreadContext context) {
Expand Down Expand Up @@ -3454,7 +3466,6 @@ private int userTeardown(ThreadContext context) {
public void releaseClassLoader() {
if (jrubyClassLoader != null) {
jrubyClassLoader.close();
//jrubyClassLoader = null;
}
}

Expand Down Expand Up @@ -5625,7 +5636,7 @@ public RubyClass getData() {
private PrintStream err;

// Java support
private final JavaSupport javaSupport;
private JavaSupport javaSupport;
private final JRubyClassLoader jrubyClassLoader;

// Object Specializer
Expand Down
12 changes: 10 additions & 2 deletions core/src/main/java/org/jruby/RubySymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -996,8 +996,7 @@ public static final class SymbolTable {
public SymbolTable(Ruby runtime) {
this.runtime = runtime;
this.loadFactor = DEFAULT_LOAD_FACTOR;
this.threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
this.symbolTable = new SymbolEntry[DEFAULT_INITIAL_CAPACITY];
reset();
}

// note all fields are final -- rehash creates new entries when necessary.
Expand Down Expand Up @@ -1337,6 +1336,15 @@ public int size() {
return size;
}

public void clear() {
reset();
}

private void reset() {
this.threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
this.symbolTable = new SymbolEntry[DEFAULT_INITIAL_CAPACITY];
}

private SymbolEntry[] rehash() {
SymbolEntry[] oldTable = symbolTable;
int oldCapacity = oldTable.length;
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/org/jruby/embed/ScriptingContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,6 @@ public void terminate() {
LocalContextProvider provider = getProvider();
if (provider.isRuntimeInitialized()) {
provider.getRuntime().tearDown(false);
provider.getRuntime().releaseClassLoader();
}
provider.terminate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,12 @@ private static boolean isURI(String path) {
return false;
}

// Clear caches and release resources
public void tearDown() {
loadedFeaturesIndex.clear();
loadedFeaturesSnapshot.clear();
}

enum Suffix {
RUBY(".rb", ResourceLibrary::new),
CLASS(".class", ClassResourceLibrary::new),
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/org/jruby/runtime/load/LoadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1009,4 +1009,9 @@ public String getPathForLocation(String filename) {

return filename;
}

public void tearDown() {
loadedFeatures.clear();
librarySearcher.tearDown();
}
}

0 comments on commit b43d1dc

Please sign in to comment.