Solved My GamePlugin is very laggy

Discussion in 'Plugin Development' started by Marsi77, May 26, 2014.

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

    Marsi77

    Hi guys I have code the last 3 weeks on my first plugin but I get some problems.
    1. The plugin is very laggy, always when some people play around the server needs 2GB Ram more than normal.
    2. The Scoreboard is sometimes flashing.
    3. The Inventory GUI does not work (At the EventListener).
    If someone can help me or have tips for the performance, it would be very helpful. Sorry for my bad English [sheep] and Sorry that the messages are written in German. :)

    Classes:
    main: http://pastebin.com/fkdBx0Qc
    Eventlistener: http://pastebin.com/fiQbW2eb
    Battlefield Functions: http://pastebin.com/FLqvSEJs
    Scoreboard/selectWeapon: http://pastebin.com/UZknzAit
     
  2. Offline

    mrkirby153

    Marsi77

    Where/when is the plugin laggy? What is the plugin doing when the server starts to lag?
     
  3. Offline

    ZeusAllMighty11

    Memory leaks galore
    [​IMG]
     
    Garris0n and Obnoxious_Ninja like this.
  4. Offline

    Smerfa

    meh, why you can't use objects? :<
    Like
    Code:
    public class Area
    {
        private List<String> playersInArena
     
    // + constructors, more fields, getters, and setters + methods to control it
    }
    And same class for Player
    Code:
    public class User
    {
        private final String nickname;
        private Location oldLocation;
        private GameMode oldGameMode;
    // more fields, constructor/s, getters, setters :P + methods to control it
    }
    And then only 2 maps, for arenas and players
    Or.. if that only for minigame, you can store Users direct in Area class (in playersInArena field, instead of nicknames)

    Java is object language, so why you don't use it? :D
     
  5. Offline

    Cirno

    Because it's the fact that it's object oriented that makes it prone to memory leaks.
    When you have a player join a server and get into the game, you store the object. Sounds fine and dandy, right?
    Except when they leave. The moment they leave, they're still stuck in that map/list, occupying a lot of memory (I believe it's keeping the entire player in memory all the way down to NMS's EntityPlayer object.)
    tl;dr: Store by string name not by object.

    edit: Wow, I'm completely blind. I read this and assumed you were OP lol. Sorry. This still applies to OP though.
     
    ZeusAllMighty11 likes this.
  6. Offline

    Smerfa

    PS: you can store Player in your objects, like in this "User" object to have fast reference to player, but then you must always remove it in QuitEvent.
    Then everything will work great - you removing (own) player before bukkit do that :p
     
  7. Offline

    mythbusterma

    Do what they said, contain all that data in one object, something to the effect of PlayerData that keeps track of that,then you only need one map of HashMap<String, PlayerData>. The excessive RAM usage is likely related to that. Also make sure to remove them on PlayerQuutEvent.

    Never store player objects, always use strings that are dereferenced when needed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  8. Offline

    Smerfa

    Yep, say that to essentials (they have Player in one of class for Users if I remember :p) authors, and some other big project <3

    Why is bad with storing Player object? There is always only one Player object for whole server, so If you don't store it when player isn't on server - it's just save and easy (+ Lombok magic and you are in haven)
     
    L33m4n123 likes this.
  9. Offline

    Marsi77

    Thank guys ;)
    I will rewritte it.
    But what is the reason that the Scoreboard is flashing?
     
  10. Offline

    Garris0n

    Been there, done that...
     
    ZeusAllMighty11 likes this.
  11. Offline

    MOMOTHEREAL

    Smerfa Listing Player objects occupies way more memory in data than simply storing their UUID.
     
  12. Offline

    mythbusterma

    It Is a general preventative measure, that eay even if you do leak the memory the (relatively large) player object will not be kept in memory by the VM, only a relatively small string (which probrably wouldn't be deleted anyways). Use Strings instead, it'll help you avoid leaks.

    BUT WHAT ARE THE ODDS HE WILL LEAK MEMORY?

    Case in point.
     
  13. Offline

    Smerfa

    ... so for you, every object it's new instance of Player?
    Code:
    Player p1 = Bukkit.getPlayer("player")
    List<Player> players = new ArrayList<>();
    for (int i = 0; i < 10; i ++)
    {
        players.add(p1);
    }
    So that code will need 11 x <size of player>? INTERESTING!!!



    If you never leave you player after quit, then you can safe use this Player...
    That only bad if you want use it when player can log-out from server and you don't remove it then!
    In other way... essentials shouldn't work :<
     
Thread Status:
Not open for further replies.

Share This Page