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 › Threading and java.lang.OutOfMemoryError
Page Index Toggle Pages: 1
Threading and java.lang.OutOfMemoryError (Read 473 times)
Threading and java.lang.OutOfMemoryError
Apr 8th, 2006, 3:11am
 
This is my first attempt playing around with threading in java/processing. I have a simple animation of a bouncing ball while I attempt to launch a thread that loads a file and copies lat/lon coordinates to corresponding lat/lon float[] arrays.

The threading seems to begin without any issues, but as the coordinates are parsed and appended to float[] arrays, it eventually encounter a java.lang.OutOfMemoryError. It never seems to occur at the same point, so I don't think it's actually running out of memory. Plus I've been able to load all of the coordinates in and copy them to float arrays without threading.

Here is the threaded code:

Code:
import java.lang.Thread;

LoadData f;
Bounce b;
float[] lat_coords = new float[0];
float[] lon_coords = new float[0];

void setup(){
size(200,200);
framerate(60);
smooth();
f = new LoadData("GLBOUNDS_MIF.MIF");
b = new Bounce();
}

void draw(){
background(0);
b.draw();
}

void mousePressed(){
f.start();
}

class LoadData extends Thread{
String filename;
String[] lines_all;
boolean record = false;
boolean regionstart = false;
boolean polystart = false;
LoadData(String filename_){
filename = filename_;
lines_all = loadStrings(filename);
}

void run(){
for(int i=0; i<lines_all.length; i++){
if(lines_all[i].startsWith("DATA")){
record = true;
}
if(record){
if(lines_all[i].startsWith("REGION")){
regionstart = true;
float percent = float(i)/float(lines_all.length) *100;
println(int(percent) + "%");
} else {
String[] coords = split(trim(lines_all[i]));
if(coords.length > 1){
lat_coords = append(lat_coords, float(coords[1])*-1);
lon_coords = append(lon_coords, float(coords[0]));
}
}
}
}
println(filename + " loaded");
}
}

class Bounce{
int x,y;
float xvec,yvec;
Bounce(){
x = int(random(0,width));
y = int(random(0,height));
xvec = int(random(0,5));
yvec = int(random(0,5));
}

void draw(){
if(x > width){
xvec *= -1;
} else if(x < 0){
xvec *= -1;
}
if(y > height){
yvec *= -1;
} else if(y < 0){
yvec *= -1;
}
x += xvec;
y += yvec;
ellipse(x,y,10,10);
}
}


The file GLBOUNDS_MIF.MIF is over 385 thousand coordinates comprising country and coastline boundaries. It's 8.5mb in size, but I went ahead and uploaded a copy incase someone was actually interested in running it.
http://www.datadreamer.com/muntadas/GLBOUNDS_MIF.MIF
Re: Threading and java.lang.OutOfMemoryError
Reply #1 - Apr 8th, 2006, 4:40pm
 
if you're dealing with 385,000 coordinates and haven't adjusted the memory settings, then there's a good chance you are in fact running out of memory.

i'd first try to up the memory used (see the faq) and see if that fixes it.

assuming that fixes it, the thing to check will be if your memory use skyrockets *during* the load, but then drops way down afterwards. in that case, it means that your thread is starving the garbage collector thread, because it's making a lot of temporary objects to parse your file. in this case, adding a Thread.sleep() or Thread.yield() intermittently (say every 10k lines) inside your run() should help fix things.
Re: Threading and java.lang.OutOfMemoryError
Reply #2 - Apr 9th, 2006, 8:39am
 
Adding -Xms128m -Xmx1024m to the run.options seems to have fixed that problem right up. Thanks Smiley.
Page Index Toggle Pages: 1