Memory allocation question in events

Discussion in 'Plugin Development' started by LRFLEW, Jan 15, 2011.

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

    LRFLEW

    I have an event that when called, it goes through a while loop to check if all the items in an array fit my plugin. If in the while loop, it finds one that makes the plugin moot, it writes false to a boolean variable called at the beginning of the event as:
    Code:
    boolean right = new Boolean(true);
    The thing is, when the event finishes, I don't know if it releases the variable. Does it? How should I call it if it doesn't? Should I release it at the end of my event somehow?

    I've had experience writing programs before, but not a lot in Java, so I'm not sure how this would work.
     
  2. Offline

    axel3200

    The variable will be released when the method (or for/while/if/etc. statement) it is declared in ends, so if you declared that at the beginning of the method it will be released at the end of the method, and any changes to its value will hold until it is released (even if that change is made inside a loop/if statement/whatever).
     
  3. Offline

    Raphfrk

    Java has a garbage collector. Basically, it just lets memory leaks happen and then when the program runs out of memory, it scans for any unused objects.

    If you have no way of referring to a variable any more, then it can be freed by the JVM automatically.

    Code:
    String string = new String("First String");
    
    string = new String("Second String");
    
    The second line changes the string variable so that it references "Second String". Thus, you have no way of accessing "First String". The JVM garbage collector can then remove the variable.

    Technically, nearly all variables are references to objects (barring the primitive types), so once you change the variable so that it references something else, the object is free to be deleted.

    The only way a memory leak could happen is if you don't change the reference. If some variable somewhere references the object then it can't be deleted.

    In you code, you could use

    right = null;

    However, boolean is a primitive type, so it isn't actually a reference, the variable stores the actual true/false value.

    boolean right = new Boolean(true);

    actually does this

    "new Boolean" creates a new Boolean object and sets its value to true

    This Boolean object is assigned to right. However, right is a primitive type, so it has to get the value of the newly created Boolean object. It assigns true to right.

    At the end of the operation

    - right stores true
    - The newly created Boolean object is floating free

    If instead you used

    Boolean right = new Boolean(true);

    At the end you would have

    - right refers to a Boolean object that has the value true

    However, unless you need the extra features of Boolean, it is probably best to use boolean.
     
  4. Offline

    LRFLEW

    I'm still confused what the difference between Boolean and boolean is.

    That pretty much answers my question. Thanks
     
  5. Offline

    axel3200

    boolean is a primitive type. A boolean variable is not an object, it simply stores true or false. A Boolean variable, on the other hand, is an object.

    http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Boolean.html

    As Raphfrk said, it is advisable to use boolean as opposed to Boolean unless you need something additional from Boolean:

    Code:
    boolean right = true;
     
  6. Offline

    Afforess

    99.9% of plugin developers here should not need to overly worry about memory management with Java. Java's garbage collector should work perfectly, barring any programmer screwups.
     
Thread Status:
Not open for further replies.

Share This Page