Remove custom classes

Discussion in 'Plugin Development' started by black_ixx, Jan 20, 2013.

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

    black_ixx

    Hey,
    so my plugin contains a custom "User" class, which stores user information:
    Code:
    package org.black_ixx.war.playerStorage;
     
    import java.io.File;
     
    import org.black_ixx.war.War;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
     
    public class User {
      private War plugin;
      private String name;
      private File fileLoc;
      private FileConfiguration userFile;
      //private ConfigurationSerializable c;
    *important information variables*
     
      public User (War instance, String name) {
          plugin = instance;
          this.name = name;
          fileLoc = new File(plugin.getDataFolder() + File.separator + "Players/"+name.charAt(0), name + ".yml");
          userFile = YamlConfiguration.loadConfiguration(fileLoc);
          *important user information from config saved to variables*
      }
     
    *information methods*
    }
    Another class manages this "User"s:

    Code:
    package org.black_ixx.war.playerStorage;
     
    import java.util.Map;
     
    import org.black_ixx.war.War;
     
    public class PSHelper {
        private static War plugin;
        public static void init(War p)
        {
            plugin = p;
        }
     
     
          public static User getUser(String name) {
                return users.containsKey(name.toLowerCase()) ? users.get(name.toLowerCase()) : new User(plugin, name.toLowerCase());
              }
              public static void addUser(String name) {
                  if (!users.containsKey(name.toLowerCase())) {
                      User user = new User(plugin, name.toLowerCase());
                      users.put(name.toLowerCase(), user);
                  }
              }
              public static void removeUser(String name){
                  if (!users.containsKey(name.toLowerCase())) {
                    return;
                  }
                  PSSaver.saveAll(users.get(name.toLowerCase()));
                  users.remove(name.toLowerCase());
                 
              }
             
              public static Map<String, User> users;
    }
    
    This works perfect.
    The thing is, that I think that the remove method is not able to remove the classes.
    I think it just removes the links but the classes are still existing.
    When my server starts, it needs less than 1 GB RAM for the next (ca.) 10 hours. But on the next day, it needs more. Even if only one player is online. The classes are probably still existing.
    Does somebody knows more about this?

    Help is welcome!

    Any ideas?

    bump...

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

    Comphenix

    Don't use static references.

    If you have to use static, remember to set "users" to NULL when the plugin unloads.
     
  3. Offline

    camyono

    try to force the Garbage Collector ( System.gc() )
     
    black_ixx likes this.
  4. Offline

    black_ixx

    What happens when the plugin unloads without setting it to null?

    Thanks! I'll try this out.
     
  5. Offline

    camyono

    It will keep it's values/objects.

    "static" means that a field or method could be used state independently to the class providing it. So there is no instance of an object necessary to use it's static methods or fields.
     
  6. Offline

    black_ixx

    Will it keep them even when the server stops and starts again? :O
     
  7. Offline

    camyono

    No. Only as long as your JVM is running or you explicit destroy the information in this static field.
     
  8. Offline

    black_ixx

    Ok thanks.
    But the "User" classes should be removed with the "remove" method (I will add a loop with the System.gc(); method aswell)?
     
  9. Offline

    camyono

    To remove entries from the list you need to call remove as you do now. Check in your loop that you dont run gc() too often. It will cost some memory calling it before freeing it. Otherwise check google for the java garbage collector. Maybe you will find some interesting points.
     
  10. Offline

    black_ixx

    wow:
    Code:
    2013-01-21 17:49:33 [INFO] Garbage Collector!
    2013-01-21 17:49:33 [INFO] Memory before call:
    2013-01-21 17:49:33 [INFO] ----------------{ Performance Monitor }----------------
    2013-01-21 17:49:33 [INFO] - Memory Stats
    2013-01-21 17:49:33 [INFO]     Max memory for server: 4879 MB
    2013-01-21 17:49:33 [INFO]     Total allocated memory: 4879 MB (100%)
    2013-01-21 17:49:33 [INFO]     Free allocated memory: 4130 MB (84%)
    2013-01-21 17:49:33 [INFO]     Used allocated memory: 749 MB (15%)
    2013-01-21 17:49:33 [INFO] -----------------------{ 1.7.0 }-----------------------
    2013-01-21 17:49:33 [INFO] Time needed: 123 milliseconds!
    2013-01-21 17:49:33 [INFO] ----------------{ Performance Monitor }----------------
    2013-01-21 17:49:33 [INFO] - Memory Stats
    2013-01-21 17:49:33 [INFO]     Max memory for server: 4874 MB
    2013-01-21 17:49:33 [INFO]     Total allocated memory: 4874 MB (100%)
    2013-01-21 17:49:33 [INFO]     Free allocated memory: 4694 MB (96%)
    2013-01-21 17:49:33 [INFO]     Used allocated memory: 180 MB (3%)
    2013-01-21 17:49:33 [INFO] -----------------------{ 1.7.0 }-----------------------
     
  11. Offline

    camyono

    Thats nice :)
     
Thread Status:
Not open for further replies.

Share This Page