B4J Question When to set a variable to Null & when not to?

Discussion in 'B4J Questions' started by Mashiane, Aug 19, 2018.

  1. Mashiane

    Mashiane Expert Licensed User

    Hi there

    When is it necessary when you have defined variables to set them to null or not? example

    Code:
    Dim lst as List
    ....

    lst = 
    null
    I am assuming if you define a variable local to a sub there is no need to set it to null. Are there instances when you absolutely need to when you no longer want to use the variable?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You never need to set the variable value to Null in B4A and B4J. In B4i there are cases where you will want to set a global variable to Null to avoid cycle references.
     
    Mashiane likes this.
  3. MarkusR

    MarkusR Well-Known Member Licensed User

    Mashiane likes this.
  4. rraswisak

    rraswisak Member Licensed User

    Sorry if i bumped up this old thread, but what if in case the variable is inside loop process (while / for) ?

    Code:
    while var = true
       
    dim mp as MediaPlayer
       ...
       ...
       mp = 
    null '<--- is this necessary ? 
    loop
    if i don't use mp = null , is mp variable will always create new instant ? and taking memory usage ?
     
  5. OliverA

    OliverA Well-Known Member Licensed User

    Dim will always create a new instance. In your case,
    Code:
    mp = null
    is not required, it's just an unnecessary extra step.
    Steps:
    1) while executes
    2) A new MediaPlayer object is assigned to mp
    3) loop executes
    4) while executes
    5) A new MediaPlayer object is assigned to mp
    a) the old MediaPlayer that was assigned to mp is now unassigned.
    6) loop executes

    When steps 4-6 execute, yes, multiple unassigned MediaPlayer objects may start using up memory, but that is only until the next garbage collection cycle is executed, at which time, they will be removed. When you use
    Code:
    mp = null
    you are just assigning null to mp, but the MediaPlayer object that used to be assigned to it still exists, it just now is unassigned and will be removed by the garbage collector once it kicks in. But since the dim does the same thing (it unassignes the the currently assigned MediaPlayer object and readies it for garbage collection), the mp=null becomes unnecessary.

    Finally, once the loop exits, any dimmed variables will go out of scope (are unassigned) and are therefore available for garbage collection. What confuses a lot of people is that when they profile such a loop, memory seems to go up, but that is just because garbage collection is not instantaneous.
     
    rraswisak likes this.
  6. rraswisak

    rraswisak Member Licensed User

    Thank you @OliverA , that was very-well explanation.

    I thought setting null to variable will clear the memory but it's does not, the old defined variable will be list in garbage collection as unassigned variable and once when garbage collection reach the maximum then the garbage collector will do the job to clear and free all unnecessary memory space, is that right ?

    This is exactly what i'm worried, i always watching memory usage of my B4J app while running. The memory usage was increase slowly but very significant, once ever reach almost 1,5gb of RAM (i have 16gb installed of RAM on my PC), but then it down to 500 mb and start growing again... that mean garbage collection has done his job.
     
  7. Daestrum

    Daestrum Well-Known Member Licensed User

    If you want to ensure the mediaplayer releases all it's resources call it's dispose() method (May need javaobject to do it).
     
    rraswisak likes this.
  8. rraswisak

    rraswisak Member Licensed User

    Thank you @Daestrum, I implement this way and watching memory usage stable in 350 to 600mb
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice