Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Bug] Memory Leak (Unmanaged ThreadLocal) in SpotifyApi via SIMPLE_DATE_FORMAT #450

Copy link
Copy link
@QiuYucheng2003

Description

@QiuYucheng2003
Issue body actions

Bug Description
In se.michaelthelin.spotify.SpotifyApi, the static field SIMPLE_DATE_FORMAT utilizes a ThreadLocal<SimpleDateFormat> to handle date parsing and formatting. However, there is no mechanism to call ThreadLocal.remove() to clean up the resource.

Reproduction Steps
This issue triggers when the SDK is integrated into server-side gateway applications or any environment utilizing long-lived thread pools (e.g., Spring Boot default executors, Tomcat).

Minimal reproduction concept:

ExecutorService pool = Executors.newFixedThreadPool(200);
for (int i = 0; i < 10000; i++) {
    pool.submit(() -> {
        try {
            // Each thread in the pool initializes its own SimpleDateFormat
            SpotifyApi.parseDefaultDate("2023-01-01T12:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}
// The ThreadLocalMap of all 200 core threads will permanently hold a SimpleDateFormat instance, as .remove() is never invoked.



Impact
This Unmanaged ThreadLocal (UTL) usage leads to memory leaks. In high-concurrency environments with large thread pools, the uncollected SimpleDateFormat instances will gradually inflate the heap memory, potentially leading to performance degradation or OutOfMemoryError over time.

Suggested Fix
Since the project likely uses Java 8+, the most robust solution is to replace ThreadLocal<SimpleDateFormat> with java.time.format.DateTimeFormatter. DateTimeFormatter is immutable and inherently thread-safe, eliminating the need for ThreadLocal entirely.
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.