Skip to content

Memory Leak: CraftPlayer objects leaked via Lambda capture in PlayerJoinListener? #199

@EinJOJO

Description

@EinJOJO

I think I found a memory leak where CraftPlayer objects (and their associated PlayerJoinEvent) are retained in memory long after a player has disconnected.

Image

The Lambda below captures 'event' or 'event.getPlayer()'

() -> FancyNpcs.getInstance().getVersionConfig().checkVersionAndDisplay(event.getPlayer(), true)

Steps to Reproduce

  1. Install FancyNpcs on a Paper/Spigot server.
  2. Log in as a player with the FancyNpcs.admin permission.
  3. Log out.
  4. Take a heap dump and search for instances of CraftPlayer. The instance will remain even though the player is offline.

Proposed fix

if (!FancyNpcs.getInstance().getFancyNpcConfig().isMuteVersionNotification() && event.getPlayer().hasPermission("FancyNpcs.admin")) {
    // Extract UUID to avoid capturing the Player/Event object
    final java.util.UUID playerUUID = event.getPlayer().getUniqueId();
    
    FancyNpcs.getInstance().getScheduler().runTaskAsynchronously(() -> {
        org.bukkit.entity.Player player = org.bukkit.Bukkit.getPlayer(playerUUID);
        if (player != null && player.isOnline()) {
            FancyNpcs.getInstance().getVersionConfig().checkVersionAndDisplay(player, true);
        }
    });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FancyNpcsEverything related to the FancyNpcs plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions