Class JsonIoCleanup

java.lang.Object
com.cedarsoftware.io.JsonIoCleanup

public final class JsonIoCleanup extends Object
Utility class for managing JsonIo resource cleanup, particularly ThreadLocal resources that could cause memory leaks in application servers or long-running applications.

This class provides centralized management of ThreadLocal cleanup for JsonIo components. It's especially important to call cleanup methods in web applications, application servers, or any environment where threads are reused.

Usage Examples:


 // Manual cleanup after processing
 try {
     Object result = JsonIo.toJava(jsonString).asType(MyClass.class);
     // process result
 } finally {
     JsonIoCleanup.clearThreadLocals();
 }
 
 // In a servlet filter or request interceptor
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
     try {
         chain.doFilter(request, response);
     } finally {
         JsonIoCleanup.clearThreadLocals();
     }
 }
 
 // Spring Boot example - request interceptor
 @Component
 public class JsonIoCleanupInterceptor implements HandlerInterceptor {
     @Override
     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
         JsonIoCleanup.clearThreadLocals();
     }
 }
 
Author:
Claude Code AI Assistant
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Method Details

    • clearThreadLocals

      public static void clearThreadLocals()
      Clears all ThreadLocal resources used by JsonIo components.

      This method should be called when you're done processing JSON in the current thread to prevent memory leaks. It's particularly important in application servers, web applications, or any environment where threads are reused (thread pools).

      When to call this method:

      • After completing JSON processing in a web request
      • In servlet filters or request interceptors
      • At the end of background job processing
      • Before returning threads to thread pools
      • In application shutdown hooks

      Thread Safety: This method is thread-safe and only affects ThreadLocal resources in the calling thread.

    • cleanup

      public static void cleanup()
      Alternative method name for clearing ThreadLocal resources. This is an alias for clearThreadLocals() that may be more intuitive for some use cases.
    • hasThreadLocalResources

      public static boolean hasThreadLocalResources()
      Checks if the current thread has any ThreadLocal resources that need cleanup.

      This method can be useful for debugging memory leak issues or for monitoring applications to ensure proper cleanup is happening.

      Returns:
      true if ThreadLocal resources are present, false otherwise
    • getThreadLocalInfo

      public static String getThreadLocalInfo()
      Provides information about ThreadLocal resource usage for monitoring and debugging.

      This method returns a human-readable string describing the ThreadLocal resources that would be cleaned up by calling clearThreadLocals().

      Returns:
      description of ThreadLocal resources, useful for debugging
    • autoCleanup

      public static JsonIoCleanup.ThreadLocalCleaner autoCleanup()
      Convenience method for use in try-with-resources blocks.

      This method returns a JsonIoCleanup.ThreadLocalCleaner that can be used in try-with-resources statements to ensure automatic cleanup.

      
       try (ThreadLocalCleaner cleaner = JsonIoCleanup.autoCleanup()) {
           Object result = JsonIo.toJava(jsonString).asType(MyClass.class);
           // ThreadLocal resources are automatically cleaned up when exiting this block
           return result;
       }
       
      Returns:
      a ThreadLocalCleaner for automatic cleanup