Solved Garbage Collection: will this benefit?

Discussion in 'Plugin Development' started by blue1, Oct 10, 2016.

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

    blue1

    I was making a plugin today when a thought occurred to me. I'm well aware that Java is really good about garbage collection and disposal, but I wondered if perhaps it could be improved with little bits of code.

    For instance, in a Cmds class, I had this bit of code:

    Code:
    List<String> words = new ArrayList<String>();
    for(String each:Main.plugin.getConfig().getStringList("Blacklist")){
        words.add(each);
    }
    words.add(args[1]);
    Main.plugin.getConfig().set("Blacklist", words);
    p.sendMessage(Main.plugin.prefix+ChatColor.GOLD+"You have blocked the word "+ChatColor.RED+args[1]);
    words = null;
    Does setting the ArrayList "words" to null destroy it and remove it from memory? Does not setting it to null end up with the creation of a new ArrayList every time someone runs the command? Because of something like this happened on the PlayerMoveEvent, then there could be billions of ArrayLists in short order, especially on a busy server.
    Just wondering about optimizations. :)
     
  2. @blue1
    Yes, that results in an ArrayList being created multiple times, but not because the Java Garbage collector is bad, but because the code is bad. Optimally, that ArrayList should be a Field in the class, as it is the same for whoever is doing it and only updated at certain occasions (for example if you have a reload command).
     
  3. Offline

    blue1

    An external ArrayList then, and referenced from within the method? I've done that in the past, and usually do. But I happened to try this today, and it occurred to me that it might get heavy.
     
  4. @blue1
    Yes, that is the optimal way to go, as it won't be remade every time, and instead be remade only when it needs to
     
    blue1 likes this.
  5. Offline

    blue1

    Would setting it to null in the end actually benefit though, if the arraylist were to be initialized within the method? (I already have moved it out, this is now more hypothetical.)
     
  6. Offline

    Zombie_Striker

    No, it would not. As long as the instance will never be used again (which should be the case), setting it to null would just be adding extra lines.
     
    blue1 likes this.
  7. Offline

    I Al Istannen

    @blue1
    @Zombie_Striker should be correct. Java is quite good at destroying obviously not needed variables. If you declare it in the method, it's scope is the method. Once the scope it left (and you didn't save a reference somewhere), there is no way this variable could be reached. Which means java's GC should collect it. Correct me if this is wrong, but I think it is correct.
     
    blue1 likes this.
  8. Offline

    mythbusterma

    @I Al Istannen

    Indeed, and actually putting temporary things explicitly in the method or inner blocks scope improves this, as the compiler can prove that it won't be used elsewhere if it's not assigned.
     
    I Al Istannen and blue1 like this.
  9. Offline

    blue1

    Thank you all for your help! I'll mark this as solved now. :)
     
Thread Status:
Not open for further replies.

Share This Page