omcougar
Messages postés152Date d'inscriptionmardi 4 mai 2004StatutMembreDernière intervention 8 octobre 2008
-
31 août 2006 à 14:30
Twinuts
Messages postés5375Date d'inscriptiondimanche 4 mai 2003StatutModérateurDernière intervention14 juin 2023
-
31 août 2006 à 15:27
Bonjour,
Imaginons que je crée un objet a partir d'un classe contenant des threads, et tout un tas de variables... pui un peu plus loin je remplace mon objet par un nouveu provenant de la meme classe
Main{
MyClass C;
C=new MyClass("toto", "tata");
..........
..........
C=new MyClass("titi", "tutu");
}
Est on certain que toute la mémoire et le thread alloué lors de la prmière création sont supprimés?
Merci.
PS: pour ceux qui ne comprenent pas mon code vous pouvez remplacer ("toto", "tata") par ("A","B")
super_toinou
Messages postés764Date d'inscriptionmardi 25 mai 2004StatutMembreDernière intervention 8 mars 20116 31 août 2006 à 14:35
Oui, mais tu sais pas quand !!! C est le principe du garbagecollector, tu peux néanmoins l appeler dynamiquement si tu veux
pour savoir comment ca marche :
http://gfx.developpez.com/tutoriel/java/gc/ +++ Toinou
Twinuts
Messages postés5375Date d'inscriptiondimanche 4 mai 2003StatutModérateurDernière intervention14 juin 2023111 31 août 2006 à 15:27
Salut,
pour completer si tu
as dans tes variables des vecteur et autres truc du gout tu peux
definir la methode finalize(destructeur en java herité de Object)
exemple :
public class Toto{
Vector vec = ....
.....
protected void finalize() throws Throwable{
vec.clear();
vec = null;
}
}
ainsi quand le finalizer passera le menage sera complet cela dis tu peux force le menage toi meme :
System.runFinalization();//active la finalization des objets
//ou
//Runtime.getRuntime().runFinalization();//active la finalization des objets
System.gc();//invoque le garbage collector
void java.lang.Runtime.runFinalization()
Runs the finalization methods of any objects pending finalization.
Calling this method suggests that the Java virtual machine expend
effort toward running the
finalize
methods of objects that have been found to be discarded but whose
finalize
methods have not yet been run. When control returns from the method
call, the virtual machine has made a best effort to complete all
outstanding finalizations.
The virtual machine performs the finalization process automatically as needed, in a separate thread, if the
runFinalization
method is not invoked explicitly.
The method System.runFinalization() is the conventional and convenient means of invoking this method.
* See Also: : <dd>java.lang.Object.finalize()
pour le gc tu peux faire un truc plus complet :
public class GC {
private static final Runtime
RUNTIME
= Runtime.getRuntime();
private GC() { }
public static void runGC() {
long usedMem1 usedMemory(), usedMem2 Long.MAX_VALUE;
for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++i) {