Memory management

HotShoe

Well-Known Member
Licensed User
Longtime User
I thought it would be interesting to discuss memory management in java and in b4a in particular. I come from more than 30 years of allocating and deallocating memory for pointers, objects, etc. In java it is generally not necessary, and is widely frowned upon to do memory optimizations. The garbage collector handles that stuff as needed. I still have questions though, even after reading through the whitepapers for java memory and garbage collection.

In my current file manager app I am using a multi-dimensional array of labels to list information about each file. The array holds up to 1000 sets of 6 labels (name, size, date, time, permissions, and a bitmap). They are then displayed on a scrollview.

Each time the user changes directories, I clear the current contents of all the labels and assign null to each label. I understand that this does not release the labels, but it does release the memory held by their contents (I think).

So the question I have is: Is there a better way to do this? The garbage collector won't mess with any object that is still referenced, and all of the labels in the array are always referenced. So, as I understand it, unless I clear the contents each time, I get something like:

A user goes into a dir with 300 .mp3's in it, does something, then goes to a dir with 5 entries in it. Unless I clear the old entries, I still have 295 entries full of stuff I no longer need that the garbage collector will ignore.

Am I understanding this correctly so far? No, I can't do what I need with a listview, it isn't flexible enough to display different colors for different entry types, etc.

Suggestions?

--- Jem
 

warwound

Expert
Licensed User
Longtime User
Suggestions?

This is where using a 'proper' ListView is the only real solution.
The ListView's Adapter takes care of so many things and displaying 300+ complex items is no problem.

Did you see my post here: http://www.b4x.com/forum/basic4android-getting-started-tutorials/17708-custom-listview-library.html?

I documented the differences between using a ScrollView and a custom ListView and then gave some example code to help build a custom ListView.

You'd need to create in this order:
  • A basic list item in XML - don't worry too much about style for now.
    A LinearLayout with 6 TextViews will do.
  • A java class with 6 data members - each member holding one of the strings to display in a list item.
  • An ArrayList of your java class type.
  • A custom ArrayAdapter powered by the ArrayList.
  • A ListView powered by your custom ArrayAdapter.

Wrap that all in a B4A library and it should handle everything you throw at it.
The physical size of your XML list item and the physical size of the device screen dictate how many list items will exist in memory at any one time.
That's the biggest memory hog taken care of.

The amount of memory used to contain say 500 instances of your java (data only) class should be no problem.

Clear and re-populate the ArrayList and the ListView will display whatever data is in the ArrayList.

Sounds tricky and i was put off by the fact that it sounds so complicated but work through my example and you'll probably find it worth the effort.

Martin.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Each time the user changes directories, I clear the current contents of all the labels and assign null to each label. I understand that this does not release the labels, but it does release the memory held by their contents (I think).

So the question I have is: Is there a better way to do this? The garbage collector won't mess with any object that is still referenced, and all of the labels in the array are always referenced. So, as I understand it, unless I clear the contents each time, I get something like:

A user goes into a dir with 300 .mp3's in it, does something, then goes to a dir with 5 entries in it. Unless I clear the old entries, I still have 295 entries full of stuff I no longer need that the garbage collector will ignore.

Am I understanding this correctly so far? No, I can't do what I need with a listview, it isn't flexible enough to display different colors for different entry types, etc.

Suggestions?

--- Jem
For most of the part I agree with Martin about the ListView but I think your question is more about the data structure holding the data?

The GC will probably be invoked whenever the OS thinks it is running out of memory, depending on how many, and how fast operations you do (for e.g. switching really fast between large directories) you will take a performance hit. In real case scenarios this might not matter.

I am not sure why you need to create (and hold) an array of so many labels.
Use a List with a custom type (lets say 6 labels), and on changing directories do a List.Clear to remove all of its contents. (In order to remove references to the label you will have to remove them from the parent as well). This should clear both references to the labels.

This is just my HO, as I have tried to understand it.
 
Upvote 0

HotShoe

Well-Known Member
Licensed User
Longtime User
This is where using a 'proper' ListView is the only real solution.
The ListView's Adapter takes care of so many things and displaying 300+ complex items is no problem.

Did you see my post here: http://www.b4x.com/forum/basic4android-getting-started-tutorials/17708-custom-listview-library.html?

Martin.

Thanks Martin, I went through that thread and think that is indeed an excellent way to handle this. I'll play with your examples and get a feel for the needs for this project.

I learned long ago that to learn a language it's best (for me) to start with it's file handling, since that forces you to learn the rest. I keep underestimating java, and I am trying to get over a strong dislike for it from years of thinking of it as just a script language. This filemanager is basically a test for my file handling library, and it certainly is teaching me different ways of thinking.

--- Jem
 
Upvote 0

HotShoe

Well-Known Member
Licensed User
Longtime User
I am not sure why you need to create (and hold) an array of so many labels.
Use a List with a custom type (lets say 6 labels), and on changing directories do a List.Clear to remove all of its contents. (In order to remove references to the label you will have to remove them from the parent as well). This should clear both references to the labels.

Directories can be huge even in android, but especially in windows machines. Since the files library fully supports samba I have to expect to support large directories.

It's not the storage of the dir entries that is the problem, it's the display and interaction with the user. A stock listview just won't do it. I already use custom types to handle the entries, but getting them to the screen in a slick, useable way is the challenge.

thanks,

--- Jem
 
Upvote 0
Top