Instance duplicate in memory after reload.

Discussion in 'Plugin Development' started by DanielTheDev, Apr 24, 2018.

Thread Status:
Not open for further replies.
  1. I'm using a initializer in the onEnable(), that will call the static contructor of the Lib class and set the 'public static final' variables. But I just found out that when I reload the server all the stored variables in the Lib are dudplicated and still uses memory.

    Can someone tell me what I'm doing wrong and how to fix this.

    Code:
        public void onEnable() {
            PluginLib.init();
        }
    And this is the library class.

    Code:
        public final class PluginLib {
            public static final (instace) name;
           
           
            static {
                name = new (instance);
            }
           
            public static void init() {}
        }
     
  2. Online

    timtower Administrator Administrator Moderator

    @DanielTheDev Don't use static, it doesn't get cleaned by Bukkit.
     
  3. @timtower Thanks for telling.
    I got a question, How can I access variables without any static variable then?
    And my second question is, I got a class that binds two varaibles like key -> value.
    So every instance has one key and one value. But the instances still remain in the memory and I have no idea how to delete it.
     
  4. Online

    timtower Administrator Administrator Moderator

    Constructors and passing instances around.
    What does it do anyways?
     
  5. Sorry, I don't exactly undersand what you mean with 'Constructors and passing instances around, and how I can access the Lib variables.
    '. And the class to bind variables helps me returning multiple values at once.

    Tag class:
    Code:
    public class Tag<K, V> {
    
        private V v;
        private K k;
    
        public Tag(K k, V v) {
            this.v = v;
            this.k = k;
        }
    
        public Tag() {}
    
        public V getV() {
            return v;
        }
    
        public void setV(V v) {
            this.v = v;
        }
    
        public K getK() {
            return k;
        }
    
        public void setK(K k) {
            this.k = k;
        }
    
        public String toString() {
           try {
    
                StringBuilder string = new StringBuilder();
    
                ArrayList<String> list = new ArrayList<String>();
    
                Class<?> type = this.getClass();
                while(true) {
    
                    for(Field field : type.getDeclaredFields()) {
    
                        field.setAccessible(true);
                        String value_name = field.getName().replace("v", "value").replace("k", "key");
                        Object var_value = field.get(this);
    
                        if(var_value instanceof String) var_value = "\""+var_value+"\"";
                        list.add(value_name+"="+var_value+",");
                    }
    
    
                    if(type.getSimpleName().equals("Tag")) break;
    
                    type = type.getSuperclass();
    
    
                }
    
    
                for(int i = list.size()-1; i >= 0; i--) {
    
                    string.append(list.get(i));
                }
    
               return this.getClass().getSimpleName()+"{"+string.substring(0, string.length()-1)+"}";
    
            } catch (Exception e) {
               PluginLib.exceptionManager.throwException(e);
                return "";
            }
        }
    
        public static class Three<K, V, V1> extends Tag<K, V> {
    
            private V1 v1;
    
            public Three(K k, V v, V1 v1) {
                super(k, v);
                this.v1 = v1;
            }
    
            public Three() {
                super();
            }
    
            public V1 getV1() {
                return v1;
            }
    
            public void setV1(V1 v1) {
                this.v1 = v1;
            }
        }
    
        public static class Four<K, V, V1, V2> extends Three<K, V, V1> {
    
            private V2 v2;
    
            public Four(K k, V v, V1 v1, V2 v2) {
                super(k, v, v1);
                this.v2 = v2;
            }
    
            public Four() {
                super();
            }
    
            public V2 getV2() {
                return v2;
            }
    
            public void setV2(V2 v2) {
                this.v2 = v2;
            }
        }
    
        public static class Five<K, V, V1, V2, V3> extends Four<K, V, V1, V2> {
    
            private V3 v3;
    
            public Five(K k, V v, V1 v1, V2 v2, V3 v3) {
                super(k, v, v1, v2);
                this.v3 = v3;
            }
    
            public Five() {
                super();
            }
    
            public V3 getV3() {
                return v3;
            }
    
            public void setV3(V3 v3) {
                this.v3 = v3;
            }
        }
    
        public static class Six<K, V, V1, V2, V3, V4> extends Five<K, V, V1, V2, V3> {
    
            private V4 v4;
    
            public Six(K k, V v, V1 v1, V2 v2, V3 v3, V4 v4) {
                super(k, v, v1, v2, v3);
                this.v4 = v4;
            }
    
            public Six() {
                super();
            }
    
            public V4 getV4() {
                return v4;
            }
    
            public void setV4(V4 v4) {
                this.v4 = v4;
            }
        }
    }
     
  6. Online

    timtower Administrator Administrator Moderator

    @DanielTheDev Constructors is object oriented programming.
    Please post that lib class, because the one you posted doesn't need static storage.
     
  7. Here you go.

    Code:
    public final class PluginLib {
    
        public static boolean libraryLoaded = false;
    
        public static final PlayerStalker plugin;
        public static final PluginManager pluginManager;
        public static final ExceptionManager exceptionManager;
        public static final Server server;
        public static final String serverVersion;
        public static final ConsoleCommandSender console;
        public static final PluginDescriptionFile descriptionFile;
        public static final String pluginVersion;
        public static final File dataFolder;
    
        public static final CommandManager commandManager;
        public static final EventManager eventManager;
        public static final FileManager fileManager;
        public static final GuiManager guiManager;
        public static final ServerPlaceHolder serverPlaceHolder;
        public static final PlayerList playerList;
        public static final PluginLogger pluginLogger;
        public static final CustomGuiManager customGuiManager;
        public static final Github github;
        public static UpdateChecker updateChecker;
        public static MySqlRunner mySqlRunner;
        public static final CommandData commandData;
        public static final Skulls skulls;
        public static final DataManager datamanager;
    
        static {
            libraryLoaded = false;
            subInit();
            plugin = PlayerStalker.plugin;
            exceptionManager = new ExceptionManager();
            server = plugin.getServer();
            pluginManager = server.getPluginManager();
            serverVersion = server.getVersion();
            console = server.getConsoleSender();
            descriptionFile = plugin.getDescription();
            pluginVersion = descriptionFile.getVersion();
            dataFolder = plugin.getDataFolder();
            fileManager = new FileManager(); fileManager.init();
            commandManager = new CommandManager();
            eventManager = new EventManager();
            guiManager = new GuiManager();
            serverPlaceHolder = new ServerPlaceHolder(plugin);
            playerList = new PlayerList();
            pluginLogger = new PluginLogger();
            customGuiManager = new CustomGuiManager();
            github = new Github("DanielTheJavaDeveloper", "plugin-versions");
            updateChecker = new UpdateChecker(false);
            mySqlRunner = new MySqlRunner(false);
            commandData = new CommandData();
            skulls = new Skulls();
            datamanager = new DataManager();
            libraryLoaded = true;
        }
    
        public static void init() {}
    
        public static void delete() {
            mySqlRunner = null;
            updateChecker = null;
            CustomData.delete();
        }
    
        private static void subInit() {
            Command.init();
            Files.init();
            CustomFiles.init();
            CustomData.init();
        }
    
    
        public static class Command {
    
            public static final ArrayList<CommandArgument> commands;
    
            static {
                commands = new ArrayList<>();
            }
            public static void init() {}
    
        }
    
        public static class Files {
    
            public static final ArrayList<FileClass> files;
    
    
            static  {
                files = new ArrayList<>();
            }
    
            public static void init() {}
        }
    
    
        public static class CustomFiles {
    
            public static final ArrayList<CustomGui> files;
    
    
            static  {
                files = new ArrayList<>();
            }
    
            public static void init() {}
    
        }
    
        public static class CustomData {
    
            public static List<String> permissions;
    
            public static LinkedHashMap<UUID, MySql.Result> message_log_resultList;
            public static LinkedHashMap<UUID, MySql.Result> command_log_resultList;
    
            public static long last_update_check;
            public static long last_refreshed_sec;
    
            public static boolean update_available;
    
            public static final int max_interval_sec = 500;
    
            public static ServerInfo info;
            public static ServerInfo.Version current_version;
    
            private static void init() {
                message_log_resultList = new LinkedHashMap<>();
                command_log_resultList = new LinkedHashMap<>();
                permissions = new ArrayList<String>() {{
                    for(Field field : Permissions.class.getDeclaredFields()) {
                        if(field.isAnnotationPresent(Permissions.Ignore.class)) continue;
                        try {
                            add(field.get(null).toString());
                        } catch (Exception e) {
                            PluginLib.exceptionManager.throwException(e);
                        }
                    }
                }};
            }
    
            private static void delete() {
                permissions = null;
                last_update_check = 0;
                last_refreshed_sec = 0;
                update_available = false;
                info = null;
                current_version = null;
                message_log_resultList.clear();
                message_log_resultList = null;
                command_log_resultList.clear();
                command_log_resultList = null;
            }
    
        }
    
    }
    
     
  8. Online

    timtower Administrator Administrator Moderator

    @DanielTheDev And why would that need to be static? Why not make an instance in the onEnable?
     
  9. @timtower Static instance of the Lib or not?
     
  10. Online

    timtower Administrator Administrator Moderator

  11. Then how can I access the lib without any static variables?
     
  12. Online

    timtower Administrator Administrator Moderator

    You pass the main instance around to all other classes, then you have a getter for the lib.
     
  13. Ok, Thank you for that.
    And about my other question.
    How can I delete the Tag<?,?> class after I don't need it anymore.
    Because I noticed that it is stacking up in the memory.
     
  14. Online

    timtower Administrator Administrator Moderator

  15. Almost everywhere. In non-static methods, and especially as parameters.
     
  16. Online

    timtower Administrator Administrator Moderator

    Java's garbage collector should clean it on its own.
     
  17. @timtower The problem is. it doesn't and I cannot call the garbage collector.
     
Thread Status:
Not open for further replies.

Share This Page