We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Memory leak / thread / images after system.gc()
Page Index Toggle Pages: 1
Memory leak / thread / images after system.gc() (Read 524 times)
Memory leak / thread / images after system.gc()
May 2nd, 2009, 7:02am
 
Problem:

Code works fine for a while and then freezes! At a guess i think it might be a memory link but i am quite new to java and cannot find out where the leak is. I have called system.gc() but the error still happens. This is the first time i have tried threading as well so that could be implemented wrong. Tried all sorts and everytime the program works for a while and then i get a memory error.

description:

The aim of the code is to fill 7 arrays with images from google using php/cUrl to scrape google search. Each array holds images of different colours. The thread continues to run and make sure that the arrays are full. The user can pop images from the array using the keys and the thread fills the array again.

Code(eclipse):

Code:


import processing.core.*;

public class mainprocessing extends PApplet {

imageStack[] imageStacks = new imageStack[7];
Thread[] wrappers = new Thread[7];
int x = 0;

public void setup() {
size(screen.width, screen.height);
background(0);
// start the imageStacks
imageStacks[0] = new imageStack("red");
imageStacks[1] = new imageStack("orange");
imageStacks[2] = new imageStack("yellow");
imageStacks[3] = new imageStack("green");
imageStacks[4] = new imageStack("blue");
imageStacks[5] = new imageStack("indigo");
imageStacks[6] = new imageStack("violet");
for (int i = 0; i < 7; i++)
{
wrappers[i] = new Thread(imageStacks[i]);
wrappers[i].start();
}
}

public void draw() {
try
{
fill(0,30);
rect(0,0,width,height);
//user input
if ((keyPressed == true) && (key == 'r'))
{
imageStacks[0].popStack();
}
if ((keyPressed == true) && (key == 'o'))
{
imageStacks[1].popStack();
}
if ((keyPressed == true) && (key == 'y'))
{
imageStacks[2].popStack();
}
if ((keyPressed == true) && (key == 'g'))
{
imageStacks[3].popStack();
}
if ((keyPressed == true) && (key == 'b'))
{
imageStacks[4].popStack();
}
if ((keyPressed == true) && (key == 'i'))
{
imageStacks[5].popStack();
}
if ((keyPressed == true) && (key == 'v'))
{
imageStacks[6].popStack();
}
}
finally
{
}

}
//
class imageStack implements Runnable
{
//the url to my php with the search param
String url = "my php";
//the main stack
PImage[] imgStack = new PImage[5];
//A vessel for what comes back from the web page
String[] lines;
//constructors mainly the color of the stack
imageStack(String stackColor)
{
url = url+stackColor;
}
// Main part of the thread where all the important loading of images happens
public void run()
{
while (true)
{
try
{
// if the top of the stack is empty bring all images up one place
for (int i = 4; i >= 0; i--)
{
//Keep loading new image till it is full
if ((imgStack[i] == null) || (imgStack[i].height < 0))
{
lines = loadStrings(url);
String href = lines[0];
imgStack[i] = requestImage(href, "jpg");
}

}
}
finally
{
}
//pause for a while
delay(250);
}
}

//pull an image from the top of the stack
public void popStack()
{
boolean valid = false;
int count = 4;
while(valid == false)
{
// check boolean to make sure image is valid
if ((imgStack[count] != null))
{
image(imgStack[count], 0, 0);
imgStack[count] = null;
valid=true;
//clear garbage
System.gc();
}
count--;
if (count == 0)
{
valid = true;

}
}
}

//end
}
}



This is becoming such a frustrating problem so any help would be much appreciated.

cheers
Re: Memory leak / thread / images after system.gc()
Reply #1 - May 2nd, 2009, 4:10pm
 
Couple of points
Is the ImageStack method supposed to be an inifinite loop

I suspect that the error is a concurrent memory access error because both the run() and the popStack() methods are in separate threads running and both are trying to modify the imageStack array at the same time. This could corrupt the imageStack data for the other thread

You might want to use the Java synchronized command to control access to the imageStack[] object. This might be useful.

http://www.herongyang.com/java/Synchronization-Support-in-Java-synchronized.html...
Re: Memory leak / thread / images after system.gc()
Reply #2 - May 2nd, 2009, 5:24pm
 
Thanks Quark you are a legend!!! i will have a good look at the tutorial and try and implement the technique. hopefully i can post a working version of this code tomorrow. Your help is most appreciated

cheers
Page Index Toggle Pages: 1