Android Question How memory is managed ?

Alessandra Pellegri

Active Member
Licensed User
Longtime User
For example:

B4X:
Sub Process_Globals
  Type TlistaElementi ( _
     IDx As String, _
     Title As String, _
     Posizione As Int _
   ) 
   dim Lista as list
end sub

Sub Activity_Create(FirstTime As Boolean)
   ....
   ....
  If FirstTime Then
          Lista.Initialize
  end if
end sub


sub myExperiment
   for a = 0 to 100
       dim elemento as TlistaElementi
       elemento.Posizione=a
       Lista.add(elemento)
   next
end sub

sub main
   myExperiment
   Lista.clear   //NOW MEMORY OF elementoS is FREED or not ?
end sub

Thank you
 

JordiCP

Expert
Licensed User
Longtime User
To my understanding, yes. Because you have declared the different "elemento"'s locally and none of them contained references to any global variable nor they weren't used anywhere else

It would be different if your user defined type contained references to a globally defined variable. Then, even if you clear the list, the global variables would still exist.


--EDIT---

Ups! I was answering a different thing. If these elemento's were referenced or used somewhere else, then, even if you clear the list, they would still exist. In your example, I think they will not exist anymore.
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
In Java, you have no real control on the memory management. Objects are removed from memory IF they are marked as collectible by the garbage collector (the most usual case is when there's no more reference to them anywhere) and WHEN the GC decides it's time to remove them. If the question is "Is Lista.Clear enough to mark my items as collectible by the GC?", then Yes in your example above (items are of primitive type and not globals), but No in some other cases.
 
Upvote 0

Alessandra Pellegri

Active Member
Licensed User
Longtime User
OK, but how the garbage collector can decide if something can be removed ?
Does it check if there is any reference to that object ? When nothing reference to an elemento then garbage collector remove that elemento, is it ?

Thank you
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
OK, but how the garbage collector can decide if something can be removed ?
Does it check if there is any reference to that object ?
Yes, it sweeps the heap and checks whether an object is in use or not. If the object is not referenced anywhere (nothing uses it), it frees the memory allocated to this object.
Usually the GC enters in action when an allocation fails or just before the OutOfMemory error is raised.
 
Upvote 0

Alessandra Pellegri

Active Member
Licensed User
Longtime User
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Ok, and is it possible to tune the GC or modify some settings?
I see ( http://www.cubrid.org/blog/dev-platform/how-to-tune-java-garbage-collection/ ) that in java is possible and I see that B4A uses jdk v7.

I see here : ( http://stackoverflow.com/questions/1567979/how-to-free-memory-in-java ) that in Java after using a big amount of memory you can force the GC to work instantly using system.gc(). Is there something similar in Android ?

Thank you
Under Android, the only thing that you can do is to request a garbage collection with the gc function, but it is of no use since Honeycomb.

Why do you want to do that?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
As an aside, System.gc() doesn't force the GC to do anything. It's best to think of System.gc() as a polite, quiet request to the JVM that it might think about performing GC if it feels like it. The JVM is free to ignore the request. The only thing you are guaranteed is that System.gc() won't return until either the GC has been performed or the JVM has decided to reject your request.

Anyhow, this is all on desktop Java (as are the GC tuning flags to which you linked). As Informatix pointed out, System.gc() is even less useful on Android. The GC is also untuneable, I think. If you are running into memory problems, there is some code in this thread: https://www.b4x.com/android/forum/t...-to-check-memory-available.58925/#post-371102 that might help you diagnose your issues.
 
Upvote 0
Top