I have a memory leak, any suggestions?

Discussion in 'Plugin Development' started by danielmiles, Apr 5, 2013.

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


    im 100% i have a memory leak somewhere in my plugin im just not 100% what is causing it and it happens at somewhat random times. Any suggestion on finding/avoiding a memory leak?

    listeners i use:
    - EntityDamageByEntityEvent
    - EntityDamgeEvent
    - PlayerInteractEvent
    - PlayerDropItemEvent
    - PlayerJoinEvent
    - PlayerQuitEvent
    - PlayerCommandPreprocessEvent
    - FoodLevelChangeEvent
    - BlockBreakEvent
    - SignChangeEvent
    - BlockPlaceEvent

    also i heard having hashmaps with "Player" as a field is bad, is this true considering i have 5 hashmaps with Player as a field that is getting updated all the time. Any ideas to why it is causing a memory leak? my guess is the hashmaps just wanna make sure tho.
  2. Offline


    danielmiles If you're storing references to Players after they have logged out, then yes, that's definitely a probable cause of the memory leak.
  3. Storing Player in long term stuff is bad because of many reasons, replace them with String and store player's name.

    Listing what events you use doesn't really help, you'd need to post your actual code.
  4. Offline


    Taken from ferrybig's signature, it's a perfect explanation:
    Memory leaks: - Player loggs off: Javas GC can't remove it cause your list/set/other still contains it. So you still have the whole instance of the player (with the IP, the real name, the display name, the list name, the health, the potion effects, ...) even if minecraft tries to save it to disc and unloads it from ram. - Multiworld (or some other multi world plugin) unloads the world the (maybe since two or more days... but still in your list...) logged off player was in. The GC can't remove it cause there's still that player instance in your map, which is holding the world loaded, which prevents the GC from releasing RAM...
  5. Offline


    so you can run System.gc(); when they log out?
  6. Offline


    System.gc() technically isn't part of java but most jvms interpret that as a suggestion to garbage check. That means System.gc() dosen't garbage check everytime. Java automatically calls the gc when its needed so usually there is no real need to call System.gc(). The Player object still holds a refrence so the gc can't get rid of it. Instead as suggested above store a string of their name (player.getName()) and use Bukkit.getPlayer(playerNameString) to get a player object.
  7. Offline


    Why would you ever need to store the player object? ....
  8. there is almost 1 time to store player, and that is if there is no other solution, I had a dube bug inside 1 of my plugin that I could saved by storing a player for 1 tick. but normaly they never safe to store, the same goes for Locations blocks world and chunks, but they use less memory (but still bad, dont forget)Vectors are safe to store as they only contain references internally to doubles
  9. Offline


    if u feel like looking through 20 odd classes to find it that would be awesome. but from what everbody is saying im pretty sure its due to me saving the players, i didint know it would cause this effect so i thought it was all good and dandy. Ill go through it and change any maps that have players to a string for playername and see if it fixes it.
Thread Status:
Not open for further replies.

Share This Page