How to safely use Player objects in a list?

Discussion in 'Plugin Development' started by St3venAU, Apr 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    St3venAU

    I know the reasons why you can be at risk of memory leaks if you add Player objects to lists. From what I understand it's due to the fact that the Player object can not be cleared from RAM when the player leaves the server since it's still in a list.

    I have however seen people mention that you can do this safely if you do it correctly but I am not sure of what that would entail. I'm guessing that this might entail ensuring that player objects are removed from lists when the player leaves, etc.

    I have generally been happy to use the player name (or the UUID now) in lists, but today I was writing a method that requires returning a list of players that is to be used immediately. My question is, would something like this result in memory leaks:

    The method:
    Code:java
    1. List<Players> getPlayers()
    2. {
    3. List<Player> players = new ArrayList<Player>();
    4. // populate the list with Player objects here
    5. return players;
    6. }

    Calling the method:
    Code:java
    1. for(Player p : getPlayers())
    2. {
    3. // do something for each player
    4. }

    Since the List<Player> that is returned by the method is never stored to a variable (instead is simply iterated over once and never used again), does that mean that it is safe from memory leaks when the players that were in that list leave the server or can it still cause a problem?
     
  2. Offline

    ButterSquidz

    Its perfectly fine to have a list of players in a local variable. In fact, its not even that terrible to have it stored (as long as you manage logins and logouts manually). However, what I will recommend you do it check if the player is online before using his object:
    Code:java
    1.  
    2. if(!player.isOnline()){
    3. return;
    4. }
     
    St3venAU likes this.
  3. Offline

    Wizehh

    Actually, Player#isOnline() will always return null if they're not online (ironic, right?). So use a null check instead:
    PHP:
    if (player == null) return;
     
  4. Offline

    ButterSquidz

    No no, he is using a custom getPlayers() method. What if the player logged off in the middle of its execution? It won't automatically be erased. So yeah, that is necessary.
    And isOnline() can't return a null. Only either true or false.
     
  5. Offline

    Wizehh

    Oh, that's a good point. And by 'returning null' I meant it would throw a null exception.
     
  6. Offline

    coasterman10

    Don't store the Player objects at all. Even if you do all the necessary checks and remove them when they quit, it's still not a good idea. Just store their names or UUIDs.
     
  7. Offline

    St3venAU

    If its never a good idea then how do methods like getNearbyEntities, getPlayers, etc return Entity/Player lists safely?
     
  8. Offline

    WhatAaCow

    St3venAU like coasterman10 SAID ! store the name/UUID of the player in a list and get the player by
    Code:java
    1. Bukkit.getServer().getPlayer(playername);
     
Thread Status:
Not open for further replies.

Share This Page