B4J Question customlistview's memory is not released

xulihang

Active Member
Licensed User
Longtime User
I use customlistview to load tens of thousands of items with lazy load.

The layout has two textareas.


Load with empty pane:

B4X:
Sub readWorkFile(filename As String,filesegments As List,fillUI As Boolean,root As String)
   ......
        Dim segmentsList As List
        segmentsList=sourceFileMap.Get(innerFilename)
        filesegments.AddAll(segmentsList)
        If fillUI Then
            For Each bitext As List In segmentsList
                createEmptyPane(bitext)
            Next
        End If
    Next
End Sub

When visible range changed, fill the pane

B4X:
Public Sub fillPane(FirstIndex As Int, LastIndex As Int)
    If segments.Size=0 Then
        Return
    End If
    Dim ExtraSize As Int
    ExtraSize=15
    For i = Max(0,FirstIndex-ExtraSize*2) To Min(Main.editorLV.Size - 1,LastIndex+ExtraSize*2)
        Dim segmentPane As Pane
        segmentPane=Main.editorLV.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            segmentPane.Enabled=True
            If segmentPane.NumberOfNodes = 0 Then
                Dim bitext As List
                bitext=segments.Get(i)
                addTextAreaToSegmentPane(segmentPane,bitext.Get(0),bitext.Get(1))
            End If
        Else
            'not visible
            segmentPane.Enabled=False
            If segmentPane.NumberOfNodes > 0 Then
                segmentPane.RemoveAllNodes '<--- remove the layout
            End If
        End If
    Next
End Sub

When I call clv.clear, the memory is not released.



Is this because the customlistview is a global variable?
 

Harris

Expert
Licensed User
Longtime User
Don't see where you called clv - clear?
This should empty the xCLV list view where you can add newest items...
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
Don't see where you called clv - clear?
This should empty the xCLV list view where you can add newest items...

This is called in a closeProject sub.

B4X:
Public Sub closeProject
    ......
        tmTableView.ClearSelection
        tmTableView.Items.Clear
        projectTreeTableView.ClearSelection
        projectTreeTableView.Root.Children.Clear
        editorLV.Clear ' this is the customlistview
        termLV.Items.Clear
        MainForm.Title="BasicCAT"
End Sub

I expect the memory usage to go down after this "closing" sub. Normally, the clv will not load that many items. It will be around 700MB when loading 700 items.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
So if never called by your user, will the clv just grow and grow?
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
It will grow when more items are loaded. And I am asking why the memory still is not released when I call clv.clear.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
I don't think you understand how the garbage collector works. The GC does not necessarily release any heap space back to the OS. To see what the JVM is doing in terms of free heap space within the process you should use JConsole.
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
or when your USER calls clv.clear???

It is a text editor which reads text files to clv. When the user open a new file, the clv will be cleared. And when a project is closed, it will be cleared, too.
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
I don't think you understand how the garbage collector works......

Jconsole seems a professional tool. I am not familiar with java. I may need to learn more before optimizing this B4J program.
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
As loading too many items will cause large memory usage, I think the easiest way for me is to use pagination. Load 50 items for each page and the heap space will not grow.

I loaded the same number of items with a c++ ATL program and it only costs 45MB, amazing. I use autoit to inspect it and find these items belong to one control. Maybe it is part of the secret.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
I think the easiest way for me is to use pagination
That makes sense...

For example, my latest android app does this with images from the Camera (or selected) folder.
The xCLV loads the 10 most recent images for the user to select (and send).
The pager will allow to load the next 10 images, and so on.
Images are scaled down in size to dramatically reduce memory use.

 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
Can you upload a project that shows the memory issue? Did the process eventually crash?
It will show out of memory if loading too much as it will load all the items and customlistview with lazyloading still has to add an empty pane which costs memory. I have solved this using listview with lazyloading.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
I think Erel was implying that it (CLV) shouldn't do this - unless your (many) images are not scaled and excessively large (2 - 8 meg let's say).
Lazy Loading was designed (I believe) to re-cycle views and prevent OOM situations. A small example may help him uncover where it - or your use of - went sideways...

Thanks
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
Because it is a text editor, I want users can scroll to any position they want. Loading more when the scrollbar reaches end is not convenient.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…