B4J Question Memory leak

ziomorgan

Active Member
Licensed User
Longtime User
How can I figure out what causes the increase continuously until the memory block of the program ?
memory leak.png
 

jmon

Well-Known Member
Licensed User
Longtime User
How can I figure out what causes the increase continuously until the memory block of the program ?
You should post your code, otherwise we won't be able to guess. Also please tell us which libraries you are using.
 
Upvote 0

ziomorgan

Active Member
Licensed User
Longtime User
This is the libraries i'm using

LIBRERIE.png


the code is more than 6000 rows. I'm pretty sure that the problem is the Tableview.


B4X:
Sub LoadTableView
For Each FileNameToParse As String In File.ListFiles(FolderRoot & "/" & FolderWork)
     If FileNameToParse.ToUpperCase.EndsWith(ElaboraSoloEstensione.ToUpperCase) Then
       AddRowTableViewKdm(FileNameToParse)
     End If
Next
End Sub

Sub AddRowTableViewKdm(TempFileName As String)
   Try
     Dim lblRow As Label
     Dim chkRow As CheckBox

     lblRow.Initialize("lblRow")
     chkRow.Initialize("chkRow")
     lblRow.Text = TempFileName
     If ParseNomeFileKdm(TempFileName.ToUpperCase, False) Then
       chkRow.Checked = True
       chkRow.Enabled = True
     Else
       lblRow.Style = "-fx-text-fill: coral;"
       chkRow.Checked = False
       chkRow.Enabled = False
     End If
     If tvKdm.GetColumnWidth(COL_FILE) > 10 Then
       lblRow.PrefWidth = tvKdm.GetColumnWidth(COL_FILE)
     Else
       lblRow.PrefWidth = 1500
     End If
     tvKdm.Items.Add(Array As Object(lblRow, chkRow))
     lblFileSelezionati.Text = "File selezionati " & FileSelezionati & "/" & tvKdm.Items.Size
   Catch
     MsgBox.DialogType = "error"
     MsgBox.Show2("ERRORE DURANTE L'AGGIUNTA DELLA RIGA <" & TempFileName & ">" & CRLF & LastException.Message, AppTitle, BtnOk, "", "")
   End Try
End Sub
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
maybe you created an infinite loop or something like that. I got a memory leak in one of my apps when I was creating and deleting large amounts of items, several times per second.

I'll just throw some things that could be the cause:
- Images that are too big
- Too many nodes created
- Too big structures, like types, arrays and maps
- too big SQL queries
- unclosed SQL pools

As you see It's quite difficult to imagine what is causing your problem without seeing more. The code you posted doesn't seem like the cause. It could be that the loop that calls "AddRowTableViewKdm" is the problem. How many items are there in your tableview?
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
about 10.000...:(...is it this the problem ?
how can i workaround ?
thk
that's the kind of info you should have provided since the beginning! :)
It could be the issue. You have 10k checkboxes and labels. Have you tried to split in pages ? Maybe you could do pages of 100 items, or implement filters?

Edit: you could try to limit the number of items, and see if you still get the memory leak.
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
you could put two buttons at the top : "<" and ">"
then you display items 1-100, that would be page 1, then the user clicks next page, you display items 101 to 200 ... and so on...
 
Upvote 0

ziomorgan

Active Member
Licensed User
Longtime User
I have done many tests with only 50 records and the situation does not change. you delay the time of the crash but at the end happens. I continue to believe that the problem is in the improper management of the memory of Tableview. in programs where no Tableview control there are never problems of this type.
renew the call for help to convert the instructions below to affix b4j.
leak.png

thanks
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
renew the call for help to convert the instructions below to affix b4j.
I'm not sure, but I think that Javafx handles the garbage collector automatically.

Are you using Javafx 8 or 7? Many or my GUI issues and slow response were fixed in javafx8. It maybe worth trying to switch to jfx8? At least, you could just install the jdk, just to try?
 
Upvote 0

ziomorgan

Active Member
Licensed User
Longtime User
I tried both Javafx7 and Javafx8 with the same result. Each time the GC acts remains a bit of memory taken up to the slow but inexorable saturation
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
I've done a quick test here.

In my test, I have 10k rows with 2 columns, with checkboxes and labels reloading every second. On my computer I don't see any memory leak, it caps at 183mb of ram.

I'm using java 8u31 x86.

I have attached the file. If you want to try.

Edit:
I tried both Javafx7 and Javafx8 with the same result. Each time the GC acts remains a bit of memory taken up to the slow but inexorable saturation
It seems that to force the garbage collector, you just need to set your variables to null after using them : https://stackoverflow.com/questions/5690309/garbage-collector-in-java-set-an-object-null

I forgot were I read that, but I've seen that it's possible to increase the heap space when running your java application with the command line, but I can't find the link where I read that.
 

Attachments

  • TestMemoryLeaktv.zip
    1.5 KB · Views: 403
Last edited:
Upvote 0

ziomorgan

Active Member
Licensed User
Longtime User
EUREKA.
In my program I added tv.SetColumns (Array ("Column 1", "Column 2")) after tv.Items.Clear and this causes the memory leak.
If you want you can also try it in your code.
Why is it so important to NOT enter SetColumn after the Clear?

Thanks not much but million for guiding me in the right direction towards solving this huge problem that I had.

Thank you thank you thank you
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Good to know, and happy to have helped you!

That means that there is no way to recreate the columns between reload. The only possibility is to create the columns at startup, then change the header if needed.

Great, good luck for the rest!

jmon
 
Upvote 0
Top