How long are new instances of Java stored? -


so lately i've had question: how long object stored when create new instance of it? i'm asking how being stored.

example:

public void inituser(player player) {         user u = new user(ally, player.getuuid());         u.sethealth(20); } 

(user being object)

so how long u stored? java clean after i'm done it? (after #sethealth called)?

thanks.

there special part of java runtime called garbage collector takes care of destroying objects when no longer in use.

application can allocate memory object via explicit , implicit ways under object creation, can not explicitly free memory.

to signal jvm object ready garbage collection, object needs unrefrenced in either of below ways :

  1. explicit unreferencing person = null; //person instance of person

  2. object goes out of scope object created inside method goes out of scope once method returns calling method. in below case once method() over, p implicitly becomes null.

    public void method(){

    person p = new person(); }

after unrefrencing, vm can reclaim memory. virtual machine can decide when garbage collect unrefrenced object. specification doesn't guarantee predictable behavior. up-to vm decide when reclaim memory unrefrenced object or might not reclaim memory @ all.

if class declares finalizer (i.e. public void finalize() method ) garbage collector execute finalize() method on instance of class before frees memory space occupied instance.

so clearly, exact time of garbage collection unpredictable.

source: http://geekrai.blogspot.com.br/2013/05/life-cycle-of-object-in-java.html


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -