Blocage puis le message d'erreur java.lang.OutOfMemoryError

Résolu/Fermé
asmakalboussi Messages postés 45 Date d'inscription dimanche 19 août 2012 Statut Membre Dernière intervention 6 décembre 2012 - 8 nov. 2012 à 00:13
asmakalboussi Messages postés 45 Date d'inscription dimanche 19 août 2012 Statut Membre Dernière intervention 6 décembre 2012 - 8 nov. 2012 à 11:39
Bonjour,

lors de l'exécution de mon programme( il sagit un nouveau algorithme evolutionnaure basé sur un nombre elevé des generations) , il ya toujours ub blocage dans une des generations.apres ce message s'affiche:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
[java] at java.util.Arrays.copyOf(Unknown Source)
[java] at java.util.Arrays.copyOf(Unknown Source)
[java] at java.util.ArrayList.grow(Unknown Source)
[java] at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
[java] at java.util.ArrayList.add(Unknown Source)

j'ai essayé plusieurs solutions proposées sur le net mais le meme souci persiste encore.Qu'est ce que je dois faire ??

A voir également:

1 réponse

Heliotte Messages postés 1491 Date d'inscription vendredi 26 octobre 2012 Statut Membre Dernière intervention 28 janvier 2013 92
8 nov. 2012 à 09:58
Bonjour asmakalboussi,
Probablement un problème de dépassement de tableau.
Soit il n'y a pas assez de mémoire pour mettre l'ensemble du tableau (cela ne se passe plus depuis que nous avons des giga octets en ram), ou tu demandes à ton tableau un élément qui n'est pas en mémoire (très souvent !).
Ton tableau contient 11 élément : t(10) ; de zéro à 10, cela fait bien 11 élément.
Tu demandes d'afficher t(11) :: erreur, dépassement.
Si ce n'est pas cela, sans voir le code, c'est très difficle.
0
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
8 nov. 2012 à 10:31
"tu demandes à ton tableau un élément qui n'est pas en mémoire"
Dans ce cas on aurait une ArrayIndexOutOfBoundsException, pas une OutOfMemoryError.

"Si ce n'est pas cela, sans voir le code, c'est très difficle."
C'est déjà ce que je disais à asmakalboussi (qui adore les doublons) dans une discussion précédente : Bloquage de programme sous eclipse et qui n'a toujours pas daigné répondre. Personellement j'abandonne, c'est un dialogue de sourd, mais si ça t'intéresse une partie du code est dans un autre doublon : Erreur introuvable :/, mais là encore on voit bien que ce qu'attend asmakalboussi c'est un miracle.
0
Heliotte Messages postés 1491 Date d'inscription vendredi 26 octobre 2012 Statut Membre Dernière intervention 28 janvier 2013 92
Modifié par Heliotte le 8/11/2012 à 10:51
Effectivement KX, je ne suis pas un spécialiste de Java, et je n'avais donc pas vu le OutOfMemory !
Tu as deux fois raison, beaucoup de code à lire, il faut absolument faire pas à pas et ou mettre en commentaire certaines partie de code pour voir d'où vient l'erreur.
0
asmakalboussi Messages postés 45 Date d'inscription dimanche 19 août 2012 Statut Membre Dernière intervention 6 décembre 2012
8 nov. 2012 à 11:04
j'adore pas les doublons KX :( . l'autre fois il ya un blocage sans aucun message d'erreur.Maintenant , il se bloque mais juste après il ya ce message d'erreur. Malheureusement , je ne suis pas un spécialiste java je suis même débutante mais je suis obligé de découvrir et programmer mon mémoire au fur et a mesure. KX je suis pas entrain de travailler sur une seule classe , c'est sur plus de 100 classes dans 10 Packages et a chaque avancement, il ya un certain truc qui cloche. En tt cas ,j'accepte votre avis(dialogue de sourd) mais franchement s'est pas ce cas ,jai juste 4 mois dans ce monde Java et avec le temps je suis capable d'être vraiment meilleure.
0
asmakalboussi Messages postés 45 Date d'inscription dimanche 19 août 2012 Statut Membre Dernière intervention 6 décembre 2012
8 nov. 2012 à 11:08
Heliotte : merci énormément pour votre réponse, comme j'ai deja dis, jai beaucoup de classe mais je fais mettre la classe qui la cause principale d'erreur .j'espere que vous pouvez maider
/*============================================================================================================
* My Adapted Version
=========================================================================

package jadex.examples.cleanerworld.multi.mocks;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import jadex.examples.cleanerworld.multi.Chargingstation;
import jadex.examples.cleanerworld.multi.Location;
import jadex.examples.cleanerworld.multi.LocationObject;
import jadex.examples.cleanerworld.multi.Obstacle;
import jadex.examples.cleanerworld.multi.TestCase;
import jadex.examples.cleanerworld.multi.Waste;
import jadex.examples.cleanerworld.multi.Wastebin;
import jadex.examples.cleanerworld.multi.utils.CommonUtil;
import jadex.examples.cleanerworld.multi.utils.GeometryUtil;
import jadex.examples.cleanerworld.multi.utils.MyConstants;
import jadex.examples.cleanerworld.multi.utils.findpath.AStarNode;
import jadex.examples.cleanerworld.multi.utils.findpath.Node;


public class MockAgent { //Begin MockAgent Class

private static final int CART_CAPACITY = 3;
private static final int PICKUP_TIME = 3;
private static final int DROP_TIME = 3;
private static final int RECHARGE_TIME = 3;
private static final double BATTERY_UNIT = 0.01;
private double battery;
private int timeout;
private boolean rechargeRequired = false;

private Location loc;
private List<LocationObject> wasteCart;
private List<LocationObject> deadPoints;
private double distwastes[]= new double[20];
private double distcharginstation[]= new double[20];


private double totalPower; //0//
private int numbObsEncountered; //1//
private int numWasteCollected; //2//
private double distAgentCollectedWaste; //3//
private double DROPcollectnearestwaste; //3//
private double distAgentChosenBin; //4//
private int DropmovingtonearestBin; //4//
private double Dropmovingtonearestcs; //5//
private double distAgentChosencs ; //5//
private double distagentobstacle ; //6//
private int Dropsafedistance;
private int NumberAvoidingobs;
private double distbestcs,distbestBin =0;

public MockAgent(double battery, int timeout, Location startLoc) {
this.battery = battery;
this.timeout = timeout;
this.loc = new Location(startLoc.getX(), startLoc.getY());

totalPower = 0;
numbObsEncountered = 0;
NumberAvoidingobs = 0;


wasteCart = new ArrayList<LocationObject>();
deadPoints = new ArrayList<LocationObject>();
}

//********************** Run the agent in an envrionment********************

public int run(TestCase env) {
int totalTime = 0;
Chargingstation[] charge = env.getStations();
Chargingstation cs1 =null;


List<Chargingstation> availableCharginstation = new ArrayList<Chargingstation>();
for(Chargingstation C: charge ){
availableCharginstation.add(C);
}

Wastebin[] wbs = env.getWastebins();
Wastebin bins =null;
List<Wastebin> availableWasteBin = new ArrayList<Wastebin>();
for(Wastebin B: wbs ){
availableWasteBin.add(B);
}
while (totalTime < timeout) { //Begin While

//=======================================
//Go to the closest charging station
//=======================================
if (battery < 0.3 || rechargeRequired) {

Chargingstation cs = (Chargingstation) GeoMockUtil.closestObj(loc, env.getStations(), deadPoints);
if(cs != null)
{
distbestcs=loc.getDistance(cs.getLocation());

}
int randomcs = (int)(Math.random() * (2-0)) + 0;
cs1 =availableCharginstation.get(randomcs);
distAgentChosencs =loc.getDistance(cs1.getLocation());
if( distAgentChosencs != distbestcs)
{
Dropmovingtonearestcs = Dropmovingtonearestcs +1;
}

totalTime += recharge(cs1, env);



} else

//==============================================================
//If the agent already collect 3wastesit must drop it to wastebin
//===============================================================

if (battery > 0.3 && wasteCart.size() == CART_CAPACITY) {

Wastebin wb = (Wastebin) GeoMockUtil.closestObj(loc, env.getWastebins(), deadPoints);
if(wb != null)
{
distbestBin=loc.getDistance(wb.getLocation());
}
int randomBin = (int)(Math.random() * (2-0)) + 0;
bins =availableWasteBin.get(randomBin);
distAgentChosenBin =loc.getDistance(bins.getLocation());
if( distAgentChosenBin != distbestBin)
{
DropmovingtonearestBin = DropmovingtonearestBin +1;
}

totalTime += dropWaste(bins, env);


}


else
//==============================================================
//If the agent don't collect 3 wastes
//===============================================================
if (battery > 0.3 && wasteCart.size() < CART_CAPACITY) {
Waste w = getNextWaste(env);
if (w == null) {
CommonUtil.print("MockAgent: all waste are cleaned!");
// drop the last waste to a wastebin
if (wasteCart.size() > 0) {
//CommonUtil.print("drop the last waste to a wastebin");
Wastebin wb = (Wastebin) GeoMockUtil.closestObj(loc, env.getWastebins(), deadPoints);
if (wb != null)
{
distbestBin=loc.getDistance(wb.getLocation());
}

int randomBin = (int)(Math.random() * (2-0)) + 0;
bins =availableWasteBin.get(randomBin);
distAgentChosenBin =loc.getDistance(bins.getLocation());
if( distAgentChosenBin != distbestBin)
{
DropmovingtonearestBin = DropmovingtonearestBin +1;
}
totalTime += dropWaste(bins, env);



//*******************************
}
// relax
relax();

// nothing else todo
break;

} else {
totalTime += pickupWaste(w, env);
if(totalTime<100){
numWasteCollected= numWasteCollected+1;

}

}

}
//==============================
//If the battery level is too low
//===============================
if (battery < 0) {
CommonUtil.print("WARNING: battery is lower than 0%: " + battery);
}
} //End While

return totalTime;
}


//**************************************************************************************
//Get the nearest waste from the current location of the agent
//*************************************************************************************
private Waste getNextWaste(TestCase env) {
Waste[] ws = env.getWastes();
Wastebin[] wbs = env.getWastebins();
List<Waste> availableWaste = new ArrayList<Waste>();

for (Waste w : ws) {
boolean cleaned = false;
for (Wastebin wb : wbs) {
if (wb.contains(w)) {
cleaned = true;
}
}
if (!cleaned) {
availableWaste.add(w);
}
}

if (availableWaste.size() == 0) {
return null;
} else {


double minDistance = Double.MAX_VALUE;
double bestdist=minDistance;
int i=0;
int k=0;
List<Waste> addedwastes = new ArrayList<Waste>();
//addedwastes = null;

Waste ret = null;
for (Waste w : availableWaste) { //begin for
if (!CommonUtil.contains(w, deadPoints) && !CommonUtil.contains(w, wasteCart)) {
double tmp = loc.getDistance(w.getLocation()); //tmp is the distance between the agent and the chosen waste
if (tmp < minDistance) {

distwastes[i]=tmp;

k++;
addedwastes.add(w);

if(tmp< bestdist){
bestdist=tmp;
}

}
i++;

}

} //end for


if(i== 0)
{
distAgentCollectedWaste = distwastes[0];

}
else{
int random = (int)(Math.random() * (i-0)) + 0;

distAgentCollectedWaste = distwastes[random];
if(distwastes[random] != bestdist) // teh agent collect the nearest waste
{
DROPcollectnearestwaste=DROPcollectnearestwaste+1;
}
ret =addedwastes.get(random);
}
return ret;

}
}






//**************************************************************************************
// Pick-up a waste
//*************************************************************************************

private int pickupWaste(Waste w, TestCase env) {
// move to the location of the waste
int cost = moveTo(w.getLocation(), env);

// pickup wate
double estimateBatteryLeft = battery - BATTERY_UNIT * 3.0;
if (cost > 0 && estimateBatteryLeft > 0) {
CommonUtil.print("MockAgent: picking up waste: " + w);

wasteCart.add(w);
cost += PICKUP_TIME;

// battery consumation
battery -= BATTERY_UNIT * 3;

// for monitoring & GA
totalPower += BATTERY_UNIT * 3;

return cost;
} else if (!rechargeRequired && estimateBatteryLeft > 0) {
deadPoints.add(w);
}

return 0;
}


//**************************************************************************************
// Drop waste to wastebin
//*************************************************************************************

private int dropWaste(Wastebin wb, TestCase env) {
int cost = moveTo(wb.getLocation(), env);

double estimateBatteryLeft = battery - wasteCart.size() * BATTERY_UNIT * 3.0;
if (cost > 0 && estimateBatteryLeft > 0) {
CommonUtil.print("MockAgent: Dropping waste to " + wb);
for (LocationObject w : wasteCart) {
wb.addWaste((Waste) w);
battery -= BATTERY_UNIT * 3;
// for monitoring & GA
totalPower += BATTERY_UNIT * 3;

cost += DROP_TIME;
}
wasteCart.clear();

return cost;
} else if (!rechargeRequired && estimateBatteryLeft > 0) {
deadPoints.add(wb);
}

return 0;
}

//**************************************************************************************
// Recharge battery
//*************************************************************************************

private int recharge(Chargingstation cs, TestCase env) {
int cost = moveTo(cs.getLocation(), env);

if (cost > 0) {
CommonUtil.print("MockAgent: Recharging at " + cs);

battery = 1;
cost += RECHARGE_TIME;
rechargeRequired = false;
deadPoints.remove(cs);
return cost;
} else {
deadPoints.add(cs);
}

return 0;
}

//**************************************************************************************
// Move from current location to a target location
//*************************************************************************************

private int moveTo(Location target, TestCase env) {

double speed = 3;

// if the target is not reachable, return -1
if (!GeoMockUtil.isReachable(target, env))
return -1;



List<Location> trajectory;

List<LocationObject> obsList = GeoMockUtil.getObstaclesOnPath(loc, target, MyConstants.CRITICAL_DISTANCE, env);

if (obsList == null) {
trajectory = GeometryUtil.getStraightTrajectory(loc, target, speed);
} else {
int h = obsList.size();
//System.out.println("liste des obstacles de taille " + h);
if(h==1){
Obstacle[] obs = env.getObstacles();
List<LocationObject> availableObstacle = new ArrayList<LocationObject>();
for(LocationObject o : obsList)
{
availableObstacle.add(o);
}
for(LocationObject ob :availableObstacle){
double distobstacle = loc.getDistance(ob.getLocation());
distagentobstacle =distobstacle;
//System.out.println("the dist between the agent and obstacle = "+ distobstacle);
if (distobstacle >= MyConstants.SAFE_DISTANCE){
//System.out.println("ok no crash");
NumberAvoidingobs= NumberAvoidingobs+1;
}else
{
// System.out.println("crash!!!!!!!!!!!!");
Dropsafedistance=Dropsafedistance+1;
}


}
}
List<AStarNode> shortestPath = GeoMockUtil.getShortestPath(loc, target, env);
if (shortestPath == null) {
return -1;
}

trajectory = new Vector<Location>();
Location s = ((Node)shortestPath.get(0)).getLoc();
List<Location> subPath = GeometryUtil.getStraightTrajectory(loc, s, speed);
trajectory.addAll(subPath);

for (int i = 0; i < shortestPath.size() - 1; i++) {
s = ((Node)shortestPath.get(i)).getLoc();
Location t = ((Node)shortestPath.get(i+1)).getLoc();
subPath = GeometryUtil.getStraightTrajectory(s, t, speed);
trajectory.addAll(subPath);
}
}

int length = trajectory.size();
// approximated cost
double cost = loc.getDistance(target) / 0.02;

double estimateBatteryLeft = battery - cost * BATTERY_UNIT;

if (estimateBatteryLeft < 0 && !rechargeRequired) {
// not enough battery to reach a location
rechargeRequired = true;
return -1;
}

// for monitoring and GA
if (obsList != null) {
numbObsEncountered += 1; //obsList.size();
}



// move to the target location
loc.setX(target.getX());
loc.setY(target.getY());

// update battery
battery -= cost * BATTERY_UNIT;

// for monitoring & GA
totalPower += BATTERY_UNIT * cost;

return length;
}

private void relax() {

}

//********************
//Getter Return
//********************

//0//

public double getTotalPower() {
return totalPower;
}

//1//
public int getNumbObsEncountered() {
return numbObsEncountered;
}
//2//
public int getnumWasteCollected() {
return numWasteCollected;
}
//3//
public double getDROPcollectnearestwaste()
{
return DROPcollectnearestwaste;
}

//4//
public double getDropmovingtonearestBin()
{
return DropmovingtonearestBin;
}
//5//
public double getDropmovingtonearestcs(){
return Dropmovingtonearestcs;
}
//6//
public double getNumberAvoidingobs()
{
return NumberAvoidingobs;
}


} //End MockAgent
=========================================================================
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
[java] at java.util.Arrays.copyOf(Unknown Source)
[java] at java.util.Arrays.copyOf(Unknown Source)
[java] at java.util.ArrayList.grow(Unknown Source)
[java] at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
[java] at java.util.ArrayList.add(Unknown Source)
[java] at jadex.examples.cleanerworld.multi.mocks.MockAgent.recharge(Unknown Source)
0
Heliotte Messages postés 1491 Date d'inscription vendredi 26 octobre 2012 Statut Membre Dernière intervention 28 janvier 2013 92
8 nov. 2012 à 11:19
Tu n'as pas un débugger dans ton programme Java, car là, c'est beucoup et je ne suis pas un spécialiste non plus !
Peut-être quelqu'un d'autre ?
0