diff --git a/java/fury-core/src/main/java/org/apache/fury/ThreadLocalFury.java b/java/fury-core/src/main/java/org/apache/fury/ThreadLocalFury.java index 67928cae37..dd98027878 100644 --- a/java/fury-core/src/main/java/org/apache/fury/ThreadLocalFury.java +++ b/java/fury-core/src/main/java/org/apache/fury/ThreadLocalFury.java @@ -247,7 +247,7 @@ public Object deserializeJavaObjectAndClass(FuryReadableChannel channel) { @Override public void setClassLoader(ClassLoader classLoader) { - setClassLoader(classLoader, StagingType.SOFT_STAGING); + setClassLoader(classLoader, StagingType.STRONG_STAGING); } @Override diff --git a/java/fury-core/src/main/java/org/apache/fury/pool/FuryPooledObjectFactory.java b/java/fury-core/src/main/java/org/apache/fury/pool/FuryPooledObjectFactory.java index 1bc03403b2..872fd459bb 100644 --- a/java/fury-core/src/main/java/org/apache/fury/pool/FuryPooledObjectFactory.java +++ b/java/fury-core/src/main/java/org/apache/fury/pool/FuryPooledObjectFactory.java @@ -78,11 +78,7 @@ public FuryPooledObjectFactory( this.furyFactory = furyFactory; this.factoryCallback = factoryCallback; classLoaderFuryPooledCache = - CacheBuilder.newBuilder() - .weakKeys() - .softValues() - .expireAfterAccess(expireTime, timeUnit) - .build(); + CacheBuilder.newBuilder().expireAfterAccess(expireTime, timeUnit).build(); } public Fury getFury() { diff --git a/java/fury-core/src/main/java/org/apache/fury/util/LoaderBinding.java b/java/fury-core/src/main/java/org/apache/fury/util/LoaderBinding.java index 79a1b6118d..3b747d2259 100644 --- a/java/fury-core/src/main/java/org/apache/fury/util/LoaderBinding.java +++ b/java/fury-core/src/main/java/org/apache/fury/util/LoaderBinding.java @@ -92,7 +92,7 @@ public ClassLoader getClassLoader() { * {@link #setClassLoader(ClassLoader, StagingType)} and {@link #clearClassLoader} should be used. */ public void setClassLoader(ClassLoader classLoader) { - setClassLoader(classLoader, StagingType.SOFT_STAGING); + setClassLoader(classLoader, StagingType.STRONG_STAGING); } /** @@ -127,8 +127,8 @@ public void setClassLoader(ClassLoader classLoader, StagingType stagingType) { fury = furyFactory.apply(classLoader); bindingCallback.accept(fury); furySoftMap.put(classLoader, new SoftReference<>(fury)); - this.fury = fury; } + this.fury = fury; break; } case STRONG_STAGING: @@ -138,8 +138,8 @@ public void setClassLoader(ClassLoader classLoader, StagingType stagingType) { fury = furyFactory.apply(classLoader); bindingCallback.accept(fury); furyMap.put(classLoader, fury); - this.fury = fury; } + this.fury = fury; break; } default: diff --git a/java/fury-core/src/test/java/org/apache/fury/ThreadSafeFuryTest.java b/java/fury-core/src/test/java/org/apache/fury/ThreadSafeFuryTest.java index 294585729a..e604bb7449 100644 --- a/java/fury-core/src/test/java/org/apache/fury/ThreadSafeFuryTest.java +++ b/java/fury-core/src/test/java/org/apache/fury/ThreadSafeFuryTest.java @@ -279,18 +279,24 @@ public void testClassGC() throws Exception { } private WeakHashMap, Boolean> generateClassForGC() { - ThreadSafeFury fury = Fury.builder().requireClassRegistration(false).buildThreadSafeFury(); + ThreadSafeFury fury1 = Fury.builder().requireClassRegistration(false).buildThreadSafeFury(); + ThreadSafeFury fury2 = + Fury.builder().requireClassRegistration(false).buildThreadSafeFuryPool(1, 2); String className = "DuplicateStruct"; WeakHashMap, Boolean> map = new WeakHashMap<>(); { Class structClass1 = Struct.createStructClass(className, 1, false); Object struct1 = Struct.createPOJO(structClass1); - byte[] bytes = fury.serialize(struct1); - Assert.assertEquals(fury.deserialize(bytes), struct1); - map.put(structClass1, true); - System.out.printf( - "structClass1 %s %s\n", - structClass1.hashCode(), structClass1.getClassLoader().hashCode()); + for (ThreadSafeFury fury : new ThreadSafeFury[] {fury1, fury2}) { + fury.setClassLoader(structClass1.getClassLoader()); + byte[] bytes = fury.serialize(struct1); + Assert.assertEquals(fury.deserialize(bytes), struct1); + map.put(structClass1, true); + System.out.printf( + "structClass1 %s %s\n", + structClass1.hashCode(), structClass1.getClassLoader().hashCode()); + fury.clearClassLoader(structClass1.getClassLoader()); + } } { Class structClass2 = Struct.createStructClass(className, 2, false); @@ -298,11 +304,13 @@ private WeakHashMap, Boolean> generateClassForGC() { System.out.printf( "structClass2 %s %s\n ", structClass2.hashCode(), structClass2.getClassLoader().hashCode()); - fury.setClassLoader(structClass2.getClassLoader()); - Object struct2 = Struct.createPOJO(structClass2); - byte[] bytes2 = fury.serialize(struct2); - Assert.assertEquals(fury.deserialize(bytes2), struct2); - fury.clearClassLoader(structClass2.getClassLoader()); + for (ThreadSafeFury fury : new ThreadSafeFury[] {fury1, fury2}) { + fury.setClassLoader(structClass2.getClassLoader()); + Object struct2 = Struct.createPOJO(structClass2); + byte[] bytes2 = fury.serialize(struct2); + Assert.assertEquals(fury.deserialize(bytes2), struct2); + fury.clearClassLoader(structClass2.getClassLoader()); + } } return map; }