Skip to content

Commit a1f50eb

Browse files
authored
feat: allow clearing all caches (#4954)
1 parent 013a62d commit a1f50eb

File tree

5 files changed

+75
-10
lines changed

5 files changed

+75
-10
lines changed

src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,19 @@ public final KeyDeserializer findKeyDeserializer(JavaType keyType,
707707
return kd;
708708
}
709709

710+
/**
711+
* Method that will drop all dynamically constructed deserializers (ones that
712+
* are counted as result value for {@link DeserializerCache#cachedDeserializersCount}).
713+
* This can be used to remove memory usage (in case some deserializers are
714+
* only used once or so), or to force re-construction of deserializers after
715+
* configuration changes for mapper than owns the provider.
716+
717+
* @since 2.19
718+
*/
719+
public void flushCachedDeserializers() {
720+
_cache.flushCachedDeserializers();
721+
}
722+
710723
/*
711724
/**********************************************************
712725
/* Public API, ObjectId handling

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4789,6 +4789,26 @@ public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visi
47894789
_serializerProvider(getSerializationConfig()).acceptJsonFormatVisitor(type, visitor);
47904790
}
47914791

4792+
/*
4793+
/**********************************************************
4794+
/* Extended Public API: caches
4795+
/**********************************************************
4796+
*/
4797+
4798+
/**
4799+
* Method that will clear all caches.
4800+
* This can be used to remove memory usage or to force re-construction cached entries after configuration changes.
4801+
* Can also be used to avoid class-loader memory leaks when reloading applications.
4802+
4803+
* @since 2.19
4804+
*/
4805+
public void clearCaches() {
4806+
_rootDeserializers.clear();
4807+
getTypeFactory().clearCache();
4808+
getDeserializationContext().flushCachedDeserializers();
4809+
getSerializerProvider().flushCachedSerializers();
4810+
}
4811+
47924812
/*
47934813
/**********************************************************
47944814
/* Internal factory methods for type ids, overridable

src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,24 @@ public final void defaultSerializeNull(JsonGenerator gen) throws IOException
12801280
}
12811281
}
12821282

1283+
/*
1284+
/********************************************************
1285+
/* Cache manipulation
1286+
/********************************************************
1287+
*/
1288+
1289+
/**
1290+
* Method that will drop all serializers currently cached by this provider.
1291+
* This can be used to remove memory usage (in case some serializers are
1292+
* only used once or so), or to force re-construction of serializers after
1293+
* configuration changes for mapper than owns the provider.
1294+
1295+
* @since 2.19
1296+
*/
1297+
public void flushCachedSerializers() {
1298+
_serializerCache.flush();
1299+
}
1300+
12831301
/*
12841302
/********************************************************
12851303
/* Error reporting

src/main/java/com/fasterxml/jackson/databind/ser/DefaultSerializerProvider.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -553,16 +553,6 @@ public int cachedSerializersCount() {
553553
return _serializerCache.size();
554554
}
555555

556-
/**
557-
* Method that will drop all serializers currently cached by this provider.
558-
* This can be used to remove memory usage (in case some serializers are
559-
* only used once or so), or to force re-construction of serializers after
560-
* configuration changes for mapper than owns the provider.
561-
*/
562-
public void flushCachedSerializers() {
563-
_serializerCache.flush();
564-
}
565-
566556
/*
567557
/**********************************************************
568558
/* Extended API called by ObjectMapper: other

src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,30 @@ public void testProviderConfig() throws Exception
409409
assertEquals(4, m._deserializationContext._cache.cachedDeserializersCount());
410410
}
411411

412+
@Test
413+
public void testClearCaches() throws Exception
414+
{
415+
ObjectMapper m = new ObjectMapper();
416+
417+
// Serialize and deserialize to fill caches
418+
final String JSON = "{ \"x\" : 3 }";
419+
Bean bean = m.readValue(JSON, Bean.class);
420+
m.writeValueAsString("test");
421+
422+
// Caches should not be empty
423+
assertNotEquals(0, m._deserializationContext._cache.cachedDeserializersCount());
424+
assertNotEquals(0, m._rootDeserializers.size());
425+
assertNotEquals(0, m._serializerProvider.cachedSerializersCount());
426+
427+
// Clear caches
428+
m.clearCaches();
429+
430+
// Caches should be empty
431+
assertEquals(0, m._deserializationContext._cache.cachedDeserializersCount());
432+
assertEquals(0, m._rootDeserializers.size());
433+
assertEquals(0, m._serializerProvider.cachedSerializersCount());
434+
}
435+
412436
// For [databind#689]
413437
@Test
414438
public void testCustomDefaultPrettyPrinter() throws Exception

0 commit comments

Comments
 (0)