B4J Question [Solved] How to free memory after use?

behnam_tr

Active Member
Licensed User
Longtime User
Hi
I have a class and inside it I have a TableView with about 100 textboxes and comboboxes loaded.
When running the program for first time, the amount of RAM consumed is about 200 MB
After opening and closing the form several times or scrolling the tableview, the amount of memory consumption reaches 1 GB, but after closing the form, this space is not released.

**All data is read from the database, then stored in the map, then entered into the textboxes and comboboxes

1. How can I manually unload all the variables to free up the used space??
2. Why are they not deleted automatically after closing the form?؟
 
Solution
I'm not convinced that there is a problem so difficult to give solution. You can limit the JVM max heap size if you like.

behnam_tr

Active Member
Licensed User
Longtime User
Unless you actually see an out of memory exception you don't need to do anything. If you do see then you have a memory leak somewhere.

No, no error or out of memory exception is received, but I want to reduce the memory consumption, is there any solution??
 
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
I'm not convinced that there is a problem so difficult to give solution. You can limit the JVM max heap size if you like.
Yes, you are right, there is no problem at the moment
My problem is high memory consumption and slow system
If I empty the map and list variables tableview.clear when closing the form, will it have an effect on memory consumption??
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
You can force the garbage collector to run with this code.


B4X:
Sub Process_Globals
    Private fx As JFX
    Private native As JavaObject

End Sub

public Sub CollectGarbageNow
    If Not(native.IsInitialized) Then
        native.InitializeStatic("java.lang.System")
    End If
    
    Log(DateTime.time(DateTime.now) & " :Collecting!!")
    native.RunMethod("gc",Null)   
End Sub

the above code should be placed in a code module and then the public function CollectGarbageNow can be called from anywhere in your code.

However, this may not solve your problem if java thinks you still have references to the objects.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Forcing the garbage collection to do its work never helps. If there is a memory leak then it is needed to fix it. If not then close task manager and forget about it.

Your system is not slow because of this 1gb ram. And if you are concerned about it then start the JVM with a memory limit.
 
Upvote 1

behnam_tr

Active Member
Licensed User
Longtime User
Forcing the garbage collection to do its work never helps. If there is a memory leak then it is needed to fix it. If not then close task manager and forget about it.

Your system is not slow because of this 1gb ram. And if you are concerned about it then start the JVM with a memory limit.
When I limit the memory, the new resources that will be added, will the previous resources be deleted automatically??
 
Upvote 0

emexes

Expert
Licensed User
I have a class and inside it I have a TableView with about 100 textboxes and comboboxes loaded.
When running the program for first time, the amount of RAM consumed is about 200 MB

**All data is read from the database, then stored in the map, then entered into the textboxes and comboboxes

How many entries does an average one of those ~50(?) comboboxes have?

Are the combobox entries just plain boring text, or are there images involved?

Do the comboboxes all have different entries, or are some of them repeated? Like, say, if 10 of the comboboxes were for selecting a country, do all 10 have the same ~200 entries?

200 MB does seem a lot, especially for textboxes (TextFields for display and data entry? or Labels for display only? or a mixture?)
 
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
How many entries does an average one of those ~50(?) comboboxes have?

Are the combobox entries just plain boring text, or are there images involved?

Do the comboboxes all have different entries, or are some of them repeated? Like, say, if 10 of the comboboxes were for selecting a country, do all 10 have the same ~200 entries?

200 MB does seem a lot, especially for textboxes (TextFields for display and data entry? or Labels for display only? or a mixture?)

Each tableview row have 1 combobox and 5 textbox
and i have about 500 - 1000 row(May be more in the future)
means 2500 - 5000 textbox in total
combobox just have text no image
all combobox have a same text (like country list)
all textbox have different text
yes TextFields for show text from db

My main problem is freeing up used RAM space
How to free this space and actually unload the variables and everything that was loaded??
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
When I limit the memory, the new resources that will be added, will the previous resources be deleted automatically??
nice question +1

However it is good to know - if there is a way to clean memory when remove an object... or there is no way... because it is a java architect...
 
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
nice question +1

However it is good to know - if there is a way to clean memory when remove an object... or there is no way... because it is a java architect...
Yes, exactly my goal is to answer this question
I have maybe 10 forms
Each form may be opened and closed multiple times
In this case, too much RAM space will be occupied by my program
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Why is this a problem? Java has a built-in garbage collector to manage memory. Why can't you just let it do its job?
So it is something that when goes to limits... decide to clean it... hmmm ok -if it is that... it covers me
 
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
Why is this a problem? Java has a built-in garbage collector to manage memory. Why can't you just let it do its job?

Garbage collector
When will this work?
When, for example, more than 80% of the memory is consumed by my program??
If I limit the amount of memory according to Earl, for example 2 GB, will the previous resources be deleted after this amount and the memory will be freed and new resources will be replaced?

So exactly what problems is this causing?
System slowness
Low memory for other applications
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
System slowness
Low memory for other applications
but in case that didn't have this garbage collector... you must you-alone create a multi-if-with-timers to do that... so your app will slow the system and the self of it.. more... i think
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
As I am thinking it... you can haveyour own limit... check if a form is showed - so do not show it again... so will limit ram consumption... the way is to make your app more intelligent .... no clean the garbages :)
 
Upvote 0
Top