Safe to store entities in arraylist?

Discussion in 'Plugin Development' started by Acer_Mortem, Aug 27, 2014.

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

    Acer_Mortem

    I know that developers always say to never store player objects in an arraylist/hashmap (for fear that they won't unload it), but is it safe to store entities in an arraylist, if you remove them from the list upon death?
     
  2. Offline

    BillyGalbreath

    It is safe to store anything, even Player objects in an arraylist.... The rumor you heard is very misleading. Just be sure you cleanup the objects when needed and no memory leaks will occur.
     
  3. Offline

    zRA1Nz

    Personally I would not store it as an object. I would serialize the entity, then store it as a string. Then un-serialize it when I need it. But thats just what I would do.
     
  4. Offline

    xTigerRebornx

    Acer_Mortem WeakReferences work well for that type of solution, and a WeakHashMap that exists that will use WeakReferences (there are ways for a "weak" Set to be made that is backed by a WeakHashMap)
     
    NathanWolf likes this.
  5. Offline

    fireblast709

    Acer_Mortem the main issue with your idea is that not all entity unloading has events (iirc it could disappear without any notice at all). Luckily for us, that is where the java.lang.ref package comes into play (slightly suggested by xTigerRebornx by mentioning WeakReferences).

    zRA1Nz Serialization is generally horribly inefficient compared to direct referencing.
     
  6. Offline

    Yekllurt

    Acer_Mortem just try out saving it as an object an see what happens, then you can decide if you like it or want to try another method, saying it direct as an object in a hashmap or arraylist is easyer for you.
    Im not 100% sure but it coud produce memory leakes like BillyGalbreath said
     
  7. Offline

    BillyGalbreath

    I didnt say it could produce memory leaks. The only way it could produce memory leaks is if you fail to manage the object when needed.

    A memory leak with the Player object occurs when you store the Player object in a hashmap and the player thens logs out. After enough players log in and out this hashmap will build up with a good amount of Player objects, even if the player's are long gone. The amount of RAM to store these Player objects will just keep increasing. A.K.A. a memory leak.

    To fix this, simply manage the Player object where needed. So, the basics of it is, when the Player object is no longer needed or valid, destroy it. You can do this simply by removing the object from the HashMap in the PlayerQuitEvent. Viola! No memory leak! \o/
     
Thread Status:
Not open for further replies.

Share This Page