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,
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
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.