Solving a Potential Memory Leak

Discussion in 'Plugin Development' started by CodeAX2, Sep 17, 2019.

  1. Offline

    CodeAX2

    Hi there! I am creating a plugin that works with scoreboards and displaying information to players. I want this information to be updated, so I have the following function enclosed in a loop that runs ever 5 ticks.
    Code:
        public void updatePlayersScores() {
            for (Player p : Bukkit.getOnlinePlayers()) {
                // Loop over every player
                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board;
                Objective obj;
    
                if (!playerBoards.containsKey(p)) {
                    board = manager.getNewScoreboard();
                    playerBoards.put(p, board);
                    obj = board.registerNewObjective("test", "dummy", "");
                } else {
                    board = playerBoards.get(p);
                    obj = board.getObjective("test");
                    Set<String> entries = board.getEntries();
                    for (String s : entries) {
                        board.resetScores(s);
                    }
                }
    
                PlayerData playerData = GriefPrevention.instance.dataStore.getPlayerData(p.getUniqueId());
    
                obj.setDisplaySlot(DisplaySlot.SIDEBAR);
                obj.setDisplayName(ChatColor.DARK_AQUA + ">>> " + ChatColor.AQUA + ChatColor.BOLD + ChatColor.ITALIC
                        + "The Tronix " + ChatColor.RESET + ChatColor.DARK_AQUA + "<<<");
    
                Score availableBlocks = obj.getScore(
                        ChatColor.GREEN + "Available Slots: " + ChatColor.AQUA + playerData.getRemainingClaimBlocks());
                availableBlocks.setScore(1);
    
                p.setScoreboard(board);
            }
        }
    I also have a hash map that contains Player to Scoreboard that makes sure I am not creating a new scoreboard every time we update. Doing /lag repeatedly on my server shows that memory tends to be leaking somewhere, and I want to make sure it isn't my own code. Does the above possibly create leaks?

    Thanks!
     
  2. Offline

    Lickymoo

    I doubt would cause any lag, and in most cases the ram checking plugins aren't super accurate. Sometimes it could just be down to the fact that you have too many plugins for your server to handle
     
    Last edited: Sep 17, 2019

Share This Page