moin,
ich hab da ein problem mit dem speicher. ich habe eine graphische komponente, die natürlich oft gezeichnet wir. nun ist das problem, dass in der zeichenmethode viel speicher gefressen wird, der aber nicht mehr freigegeben wird, was zu folgendem fehler führt:
dreimal zeichnet er mir die komponente, dann kommt den fehler.
hier ist die komponente:
in den methoden selber schreibe ich eigentlich nur in variablen, die dann übergeben werden, sprich auf die irgendwann keine referenz mehr vorhanden ist und gelöscht werden sollten.
hier noch die methoden die aufgerufen werden (ich poste einfach mal alle):
tja also der frisst halt die ganze zeit speicher, wenn die methode aufgerufen wird, gibt die aber anscheinend nicht mehr frei, da es zu besagtem fehler kommt.
hat einer eine idee, was hier zu machen ist?
danke schon mal im vorraus
ich hab da ein problem mit dem speicher. ich habe eine graphische komponente, die natürlich oft gezeichnet wir. nun ist das problem, dass in der zeichenmethode viel speicher gefressen wird, der aber nicht mehr freigegeben wird, was zu folgendem fehler führt:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
hier ist die komponente:
Code:
package ppsimulation.classes.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Vector;
import javax.swing.JPanel;
import ppsimulation.classes.CreatureField;
import ppsimulation.classes.Island;
/**
*
* @author Tristan Storch
*/
public class IslandPanel extends JPanel
{
private IslandPanel()
{
super();
}
public IslandPanel(Island island)
{
super();
this.island = island;
}
public IslandPanel(Island island, int fieldsize)
{
super();
this.island = island;
this.fieldSize = fieldsize;
}
public void Draw()
{
this.setOpaque(true);
}
public void paintComponent(Graphics g)
{
// in dieser methode wird der speicher gefressen
super.paintComponent(g);
g.setColor(this.island.getColor());
for (int i = 0; i < this.island.getX(); i++)
for (int j = 0; j < this.island.getY(); j++)
g.fillRect(i*this.fieldSize, j*this.fieldSize,
this.fieldSize, this.fieldSize);
// hier wird der speicher verbraten---------------------------------------------
// Speicherverbrauch laut taskmanager beim 1., 2., 3. schleifendurchlauf in kb:
// 18,204 21,604 24,184
for (int i = 0; i < this.island.getGenera().size(); i++)
{
// 19,152 22,576 25,860
Vector<CreatureField> buffer =
this.island.getGenera().get(i).getPopulation();
// 20,152 23,576 27,492
g.setColor(this.island.getGenera().get(i).getColor());
// 21,604 24,184 28,320
for (int j = 0; j < buffer.size(); j++)
g.fillRect(buffer.get(j).getX()*this.fieldSize,
buffer.get(j).getY()*this.fieldSize,
this.fieldSize, this.fieldSize);
// nichts dazugekommen
}
// nach dem ersten mal zeichenen: 30,140
// nach dem zweiten mal zeichenen: 40,108
// nach dem dritten mal zeichenen: 49,524
// nach dem vierten mal zeichenen: 64,480
// nach dem fünften mal zeichenen: 75,476
// nach dem sechsten mal zeichenen: Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
// ------------------------------------------------------------------------------
}
public Dimension getPreferredSize()
{
Dimension temp = null;
if (this.island != null)
temp = new Dimension(this.island.getX()*this.fieldSize,
this.island.getY()*this.fieldSize);
return temp;
}
public int getFieldSize()
{
return this.fieldSize;
}
public void setFieldSize(int i)
{
this.fieldSize = i;
}
public Island getIsland()
{
return this.island;
}
public void setIsland(Island island)
{
if (island != null)
this.island = island;
}
private int fieldSize = 5;
private Island island;
}
in den methoden selber schreibe ich eigentlich nur in variablen, die dann übergeben werden, sprich auf die irgendwann keine referenz mehr vorhanden ist und gelöscht werden sollten.
hier noch die methoden die aufgerufen werden (ich poste einfach mal alle):
Code:
public Vector<Species> getGenera()
{
Vector<Species> temp = new Vector<Species>();
for (int i = 0; i < this.genera.size(); i++)
temp.add(this.genera.get(i).clone());
return temp;
}
Code:
public Species clone()
{
Species temp = null;
try
{
temp = new Species(this);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return temp;
}
Code:
public Species(Species species) throws WrongAssignmentException,
IllegalStartNumberException
{
this(new SpeciesConfig(species.name, species.color, 0, species.maxAge,
species.reproductionRate, species.maxAge,species.maxFood,
species.range, species.moveDiagonal, species.mortal));
for (int i = 0; i < species.population.size(); i++)
this.population.add(species.population.get(i).clone());
for (int i = 0; i < species.populationCurve.size(); i++)
this.populationCurve.add(new Integer(species.populationCurve.get(i)));
this.hunters = null;
this.prey = null;
}
Code:
public SpeciesConfig(String name,
Color color,
int startNumber,
int maxPopulation,
int reproductionRate,
int maxAge,
int maxFood,
int range,
boolean moveDiagonal,
boolean mortal)
throws WrongAssignmentException, IllegalStartNumberException
{
if (startNumber >= -1
&& maxPopulation >= -1
&& reproductionRate >= 0
&& maxAge >= -1
&& maxFood >= -1
&& range >= 0)
{
if (startNumber <= maxPopulation || maxPopulation == -1)
{
this.name = name;
this.color = color;
this.moveDiagonal = moveDiagonal;
this.mortal = mortal;
this.startNumber = startNumber;
this.maxPopulation = maxPopulation;
this.reproductionRate = reproductionRate;
this.maxAge = maxAge;
this.maxFood = maxFood;
this.range = range;
}
else throw new IllegalStartNumberException(
"Start number of Species is greater than the maximal " +
"Population", this);
}
else throw new WrongAssignmentException(
"Negative Assignment in Species " + name, this);
}
Code:
public Vector<CreatureField> getPopulation()
{
Vector<CreatureField> temp = new Vector<CreatureField>();
for(int i = 0; i < this.population.size(); i++)
temp.add(this.population.get(i).clone());
return temp;
}
Code:
public Color getColor()
{
return this.color;
}
Code:
public CreatureField clone()
{
try
{
return new CreatureField(this);
}
catch (NoBorderException ex)
{
ex.printStackTrace();
}
return null;
}
Code:
public CreatureField(CreatureField creaturefield) throws NoBorderException
{
for (int i = 0; i < creaturefield.population.size(); i++)
this.population.add(creaturefield.population.get(i));
this.x = creaturefield.x;
this.y = creaturefield.y;
this.setCompareValue();
}
Code:
protected void setCompareValue()
{
this.compareValue = this.x*borders.getY() + this.y;
}
tja also der frisst halt die ganze zeit speicher, wenn die methode aufgerufen wird, gibt die aber anscheinend nicht mehr frei, da es zu besagtem fehler kommt.
hat einer eine idee, was hier zu machen ist?
danke schon mal im vorraus