B4J Question Understanding RemoveNodeFromParent (b4x)

ilan

Expert
Licensed User
Longtime User
i spent 2 days on finding a bug in my app but then i learned something new on nodes (views).

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private mybtn As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    
    mybtn.Initialize("")
    mybtn.Text = "test"
    mybtn.TextColor = fx.Colors.Black
    MainForm.RootPane.AddNode(mybtn,0,0,100,50)
    
    Sleep(2000)
    mybtn.RemoveNodeFromParent
    Sleep(2000)
    Log(mybtn.Text)
    mybtn.Text = "test2"
    Sleep(2000)
    Log(mybtn.Text)
    Log(mybtn.Visible)
    Log(mybtn.Enabled)
    Log(mybtn.IsInitialized)
    
End Sub

logs:

Waiting for debugger to connect...
Program started.
test
test2
true
true
true

so what we can see here is that after i remove the view it is still available and i can reuse it if i want but my question is, if i have a scrollview where i load views to it and then i remove them and load again ... now if the old views are still somewhere in space will it affect the phone memory? i believe that if i still can get to that view that it is stored somewhere so it is using memory so if i have a clv and i add lots of views to it then clear the clv and add again views so it can happen that there are 1000s of views that i don't really need and they are using memory, how can i remove a view completely? or it is normal like that and i should not deal with this issue too much?

thanx, ilan
 

ilan

Expert
Licensed User
Longtime User
A view, like any other object, will be released once there are no live references to that view.

i dont understand what it means "no references", do you mean that it has no parent? or it has no properties like image for imageview or text for label?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is not 100% accurate, however you can say that if you can somehow "get" the object / view then it has live references and it will not be released.

B4X:
List1.Add(Button1) 'List1 is a global list
List1 holds a reference to Button1.
Button1 will never be released.

The garbage collector tracks all the references in the program memory. Once it finds objects with no live references = objects that you cannot reach in any way, their memory will be released.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
thank you erel for your answer, so what i understand is that if i have a CLV and i add items to it that are a collection of views and then remove them, the views are released.
now it is clear, thanx, learned something new today :)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Worth adding that in B4i, there is no garbage collector. The OS uses a simpler method of reference counting. In most cases it behaves the same, however it will not release objects with cyclic references (https://www.b4x.com/android/forum/t...instances-of-other-classes.79102/#post-501249).

actually, i am working on a b4i app but posted it on b4j forum because writing a simple example code is simpler in b4j so in b4i we need to be more careful with adding views?
i am using a lot b4xCLV in my ios apps, what would you suggest in this case to keep memory usage low?
 
Upvote 0
Top