Passing parameters vs Allocation inside function, memory-leak in java/android? -


2 different ways of doing same thing (i.e fetching messages database)

  • scenerio 1

calling class:

arraylist<string> messages = new arraylist<string>(); messages = db.getallmessage(messages); 

database class:

public arraylist<string> getallmessage(arraylist<string> m) {    m= /* messages db */    return m; } 
  • scenerio 2

calling class:

arraylist<string> messages = new arraylist<string>(); messages = db.getallmessage(); 

database class:

public arraylist<string> getallmessage() {    arraylist<string> temp = new arraylist<string>();    temp = /* messages db */    return temp; } 

my question is, in scenario 2,

  1. what happen temp arraylist variable,
  2. will remain in memory until activity destroyed ?
  3. is memory leak?

edit:

in talk video android application architecture (android dev summit 2015), adam powell mentions gc enemy , suggests use scenario 1 in case of android app.

the variable go out of scope , deallocated (as call stack shrinks).

the object variable points (the arraylist) still has reference pointing it, not collected.

it go away when no more references (such messages variable or field) exists.

this not memory leak.


in both scenarios, pointless create empty arraylist before calling function (and should asking happens once receive new one, worried temp. don't worry, it's pointless, not dangerous or "leaky").

do instead:

arraylist<string> messages = db.getallmessages(); 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -