FancyInnovations / FancyInnovations/FancyPlugins

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

Open
#199 0 comments 0 reactions 0 assignees View on GitHub
FancyNpcs
Dominant language
Java
Stars
88
Forks
109
PR merge metrics
No merged PRs in 30d

Description

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()'
https://github.com/FancyInnovations/FancyPlugins/blob/5f3bb7eb2836ac8f19fcf446857685195bad250b/plugins/fancynpcs-v2/src/main/java/de/oliver/fancynpcs/listeners/PlayerJoinListener.java#L27

## 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
```java
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);
}
});
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.