import gifAnimation.*;//
ArrayList<Gif> loopingGifs;//
void setup() {
//size(512, 414, P3D);
fullScreen(P3D,2);
frameRate(60);
loopingGifs = new ArrayList<Gif>();
}
void draw()
{
if(t&&frameCount % 30 == 0)
{
thread("post");//多线程加载机制,不需要占用主线程时间。
if(loopingGifs.size()>0&&loopingGifs.get(loopingGifs.size()-1).isLoaded())
{
t = false;
}
}
}
void post()
{
loopingGifs.add(new Gif(this, "hh.gif"));
}
Hello, everyone. This is part of my code. After I loaded multiple gifs and then removed them by loopingGifs.remove(), I found that the memory did not decrease. When I used many gifs, there was an error of no memory.Is there any good way to solve this problem?
try to overwrite them instead of adding and erasing the previous. If that doesn’t work, increase the allocated memory in preferences.
Thank you for your answer, but the workload of overwrite will be very large, and I need to rewrite most of the code.Increasing the entire memory is probably not a good idea, because I deleted it soon after it was created but the memory is still increasing
import gifAnimation.*;
ParticleSystem ps;
ArrayList<Gif> loopingGifs;
//int id=0;
boolean t = false;
void setup() {
size(1280, 720,P3D);
imageMode(CENTER);
ps = new ParticleSystem();
loopingGifs = new ArrayList<Gif>();
}
void draw() {
background(0);
if(t&&frameCount % 30 == 0)
{
thread("post");
if(loopingGifs.size()>0&&loopingGifs.get(loopingGifs.size()-1).isLoaded())
{
t = false;
//id+=1;
}
}
ps.run();
String txt_fps = String.format(getClass().getName()+ " [fps %6.2f] [frame %d] ", frameRate, frameCount);
surface.setTitle(txt_fps);
}
void mouseClicked()
{
t = true;
ps.addParticle(new PVector(mouseX,mouseY));
}
void post()
{
loopingGifs.add(new Gif(this, "g.gif"));
}
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
int id ;
float lifespan;
boolean bofang = true;
Particle(PVector l) {
// The acceleration
acceleration = new PVector(0, 0.05);
// circel's x and y ==> range
velocity = new PVector(random(-1, 1), random(-2, 0));
// apawn's position
location = l.copy();
// the circle life time
lifespan = 255.0;
}
void run() {
update();
display();
}
void update() {
// velocity.add(acceleration);
//location.add(velocity);
lifespan-=0.2;
}
boolean isDead() {
if (lifespan <= 0) {
return true;
} else {
return false;
}
}
void display() {
// border
stroke(0, lifespan);
// border's weight
strokeWeight(1);
float r = random(0,255);
float g = random(0,255);
float b = random(0,255);
// random the circle's color
fill(r,g,b, lifespan);
// draw circle
ellipse(location.x, location.y, 3, 3);
if(loopingGifs.size()>0)
{
image(loopingGifs.get(id),location.x,location.y);
if(loopingGifs.get(id).isLoaded())
{
println(loopingGifs.size(),loopingGifs.get(id).isLoaded());
if(bofang)
{
loopingGifs.get(id).play();
loopingGifs.get(id).ignoreRepeat();
bofang = false;
}
if(!loopingGifs.get(id).isPlaying())
{
// lifespan = 0;
}
// println(loopingGifs.get(id).isLooping());
}
}
}
}
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem() {
particles = new ArrayList<Particle>();
}
void addParticle(PVector pos) {
origin = pos.copy();
particles.add(new Particle(origin));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.id = i;
p.run();
if (p.isDead()) {
particles.remove(i);
loopingGifs.get(i).dispose();
loopingGifs.remove(i);
}
}
}
}
Here is all my code!