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

Mashiane

Expert
Licensed User
Longtime User
Hi there

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

B4X:
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?
 

MarkusR

Well-Known Member
Licensed User
Longtime User
Upvote 0

rraswisak

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

B4X:
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 ?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
if i don't use mp = null , is mp variable will always create new instant ? and taking memory usage ?
Dim will always create a new instance. In your case,
B4X:
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
B4X:
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.
 
Upvote 0

rraswisak

Active 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 ?

What confuses a lot of people is that when they profile such a loop, memory seems to go up

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.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
If you want to ensure the mediaplayer releases all it's resources call it's dispose() method (May need javaobject to do it).
 
Upvote 0
Top