Increased consumption of memorie

Started by canoide1592, February 21, 2014, 10:29:26 PM

Previous topic - Next topic

canoide1592

I'm putting together a live wallpaper I have a problem with memory consumption by loading memory consumption is 7mb to 13mb passing time consumption increases for a time came to 139MB and increasing following.

Might you help me? You need to help me?

Forgive the English translator google'm dealing

EgonOlsen

It's almost impossible to help here without further details. Most likely, you have a memory leak somewhere but that could be anything. In Eclipse, you can trace allocations and the heap usage in the DDMS view. Maybe that helps to identify the source of your problem.

canoide1592

#2
Quote from: EgonOlsen on February 21, 2014, 11:16:28 PM
It's almost impossible to help here without further details. Most likely, you have a memory leak somewhere but that could be anything. In Eclipse, you can trace allocations and the heap usage in the DDMS view. Maybe that helps to identify the source of your problem.

here is the code that makes me much increase memory

import java.util.ArrayList;

import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.World;
import com.threed.jpct.util.ExtendedPrimitives;

public class SistemaParticulas {


ArrayList<Part> lst;
private int quantidade; //limit of particles visible
private String textureName; //the name of texture previous loaded
private int delay; //time to create particle
private int inc;
private World world;
private SimpleVector pos; //start position
private float w; //start width
private float h; //start height
private float speed; //movement speed
private int duracao; //delay for change transparency value
boolean visible; //hide or show
private SimpleVector maxDir; //max random direction on create
private SimpleVector dir; //direction
private boolean scale; //if true particle grow up
private float maxscale; //max grow up



//pos - start position
//w, h - width and height
//textureName - The name of the texture
//quantidade- limit of particles visible
//delay - time to create particle
//speed - movement speed
//duracao - delay for change transparency value
public SistemaParticulas(World world,SimpleVector pos,float w,float h,String textureName,int quantidade,int delay,float speed,int duracao)
{
this.quantidade=quantidade;
this.textureName=textureName;
this.delay=delay;

this.world=world;
this.pos=pos;
this.w=w;
this.h=h;
this.speed=speed;
this.lst=new ArrayList<Part>();
this.duracao=duracao;
visible=true;
this.maxDir=new SimpleVector();
this.maxDir.x=0.2f;
this.maxDir.y=1.0f;
this.maxDir.z=0.2f;
this.dir=new SimpleVector();
this.dir.x=1f;
this.dir.y=-1f;
this.dir.z=1f;
this.scale=true;
this.maxscale=3f;
}

public void Release(){
lst.retainAll(lst);
}

//Set start position
public void setPos(SimpleVector pos)
{
this.pos=pos;
}

//Get start position
public SimpleVector getPos()
{
return pos;
}

//Set max random Direction
//Use a normalize vector
public void setMaxDir(SimpleVector dir)
{
maxDir=dir;
}

public SimpleVector getMaxDir()
{
return maxDir;
}

//Set initial direction
public void setDir(SimpleVector dir)
{
this.dir=dir;
}

//Get initial direction
public SimpleVector getDir()
{
return dir;
}

//Set the time counter to wait to create particle
public void setDelay(int delay)
{
this.delay=delay;
}

public int getDelay()
{
return delay;
}

public int getDuracao()
{
return duracao;
}

//Set the time counter to change to the next transparency value
public void setDuracao(int duracao)
{
this.duracao=duracao;
}

//Change the initial size
public void setSize(float w,float h)
{
this.w=w;
this.h=h;
}

//Get width
public float getW()
{
return w;
}

//Get height
public float getH()
{
return h;
}

//Call on DrawFrame
public void Update(float timeDiff)
{
if (!this.visible)
return;

inc++;

if (inc>=delay && lst.size()<quantidade)
{

inc=0;

float dx=(float)(Math.random() * (1+1)-1);
if (dx>maxDir.x)
dx=maxDir.x;

float dy=(float)(Math.random() * (1+1)-1);
if (dy>maxDir.y)
dy=maxDir.y;
float dz=(float)Math.random();
if (dz>maxDir.z)
dz=maxDir.z;




dx=dx*(dir.z);
dy=dy*(dir.y);
dz=dz*(dir.z);

SimpleVector dir=new SimpleVector(dx,dy,dz);
Part obj=new Part(dir.normalize());
lst.add(obj);
}


for (int i=0; i < lst.size(); i++)
{
Part o=lst.get(i);

if (o!=null)
{
o.Move(timeDiff);
Object3D dentro = o.getObj();

int t = dentro.getTransparency();

if (t==0)
{
o.Release();
o=null;
lst.remove(i);
}

}

}

}

//res=true - the particle will grow
//Default is true
public void SetScale(boolean res)
{
scale=res;
}

public boolean isScale()
{
return scale;
}

//Set Max scale of particle is isScale() is true
public void SetMaxScale(float max)
{
maxscale=max;
}

public float GetMaxScale()
{
return maxscale;
}


public class Part
{
private Object3D p;
SimpleVector ppos;
SimpleVector pdir;
int inc_duracao;

public Part(SimpleVector dir)
{

this.p = ExtendedPrimitives.createSprite(w, h);
this.ppos=pos;
this.p.setOrigin(ppos);
this.p.setVisibility(true);
this.p.setTransparency(100);
this.p.setTexture(textureName);
this.p.build();
this.p.compile();
p.strip();
p.forceGeometryIndices(true);
world.addObject(this.p);

inc_duracao=0;
this.pdir=dir;

}

public Object3D getObj()
{
return this.p;
}

public void Move(float timeDiff)
{
if (!visible)
return;
int t = p.getTransparency();


inc_duracao++;

if (inc_duracao>duracao)
{
inc_duracao=0;
t--;
if (t<0)
t=0;
}

if (t<=0)
t=0;
p.setTransparency(t);

if (t==0)
return;
ppos=p.getOrigin();
ppos.x=ppos.x+(pdir.x*speed*timeDiff);
ppos.y=ppos.y+(pdir.y*speed*timeDiff);
ppos.z=ppos.z+(pdir.z*speed*timeDiff);
p.setOrigin(ppos);

if (scale)
{
if (p.getScale()<maxscale)
p.scale(1.005f);
}


}

public void Release()
{
world.removeObject(p);
p.clearObject();
p=null;

}
}
}


in the render call onDrawFrame update and initialized in OnSurfaceChanged the following code initializes

SimpleVector pos = new SimpleVector(0, 0, 15);
part = new SistemaParticulas(world, pos, 30, 30, "texture", 20,
1, 10, 0);
part.SetMaxScale(4f);
part.setMaxDir(new SimpleVector(0.1f, 1f, 0.1f));

EgonOlsen

Is this actually a problem? Do you run into an OOM exception or is this "just" a felt problem of the app consuming more memory than it should? Your code does one thing that's not a very good idea on Android: It creates and removes Object3Ds all the time. It's much much better to pool them, take them from the pool if needed and put it back into the pool if they aren't needed anymore. With what you have now, you are constantly creating new instances, which also means that your app constantly reserves new native memory to upload the objects to OpenGL. This memory will get freed after the objects get garbage collected, but depending on the runtime and the rest of the app, this might happen late. In addition, you should consider to make your particles share data by cloning them from a blueprint particle like so:


Object3D newy=new Object3D(bluePrint, true);
newy.shareCompiledData(bluePrint);


So...i would try to:


  • create objects from blue prints instead of creating a new one all the time
  • pool them

canoide1592

#4
Quote from: EgonOlsen on February 22, 2014, 10:28:25 AM
Is this actually a problem? Do you run into an OOM exception or is this "just" a felt problem of the app consuming more memory than it should? Your code does one thing that's not a very good idea on Android: It creates and removes Object3Ds all the time. It's much much better to pool them, take them from the pool if needed and put it back into the pool if they aren't needed anymore. With what you have now, you are constantly creating new instances, which also means that your app constantly reserves new native memory to upload the objects to OpenGL. This memory will get freed after the objects get garbage collected, but depending on the runtime and the rest of the app, this might happen late. In addition, you should consider to make your particles share data by cloning them from a blueprint particle like so:


Object3D newy=new Object3D(bluePrint, true);
newy.shareCompiledData(bluePrint);


So...i would try to:


  • create objects from blue prints instead of creating a new one all the time
  • pool them


I have a problem with the world remuevo Object3D and particle clears me the texture. When changing settings in the speaker must also change so the different speaker load

if I remove the object did not change to another model, but to remove me I deleted the textures of the particles.
If I remove the model particles work well without modification despite changes its color



public void onSurfaceChanged(GL10 gl, int w, int h) {
if (fb != null) {
fb.dispose();
}
fb = new FrameBuffer(gl, w, h);

if (master == null) {
world = new World();
world.setAmbientLight(20, 20, 20);

sun = new Light(world);
sun.setIntensity(250, 250, 250);

loadfire();

try {
if (mNumParlante == 1)
cargarParlante1();
else if (mNumParlante == 2)
cargarParlante2();
else if (mNumParlante == 3)
cargarParlante3();
} catch (IOException e) {
e.printStackTrace();
}
world.removeAllObjects();
world.addObject(cube);


if(mDesHabParSec){
world.addObject(cube2);
world.addObject(cube3);
world.addObject(cube4);
world.addObject(cube5);
}

//CONFIGURACION DE LA CAMARA
Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.lookAt(cube.getTransformedCenter());

//SE CARGA PARTICULAS DEL FUEGO
SimpleVector pos = new SimpleVector(0, 0, 15);
part = new SistemaParticulas(world, pos, 30, 30, "texture", 20,
1, 10, 0);
part.SetMaxScale(4f);
part.setMaxDir(new SimpleVector(0.1f, 1f, 0.1f));


SimpleVector sv = new SimpleVector();
sv.set(cube.getTransformedCenter());
sv.y -= 100;
sv.z -= 100;
sun.setPosition(sv);
MemoryHelper.compact();

if (master == null) {
Logger.log("Saving master Activity!");
master = MyWallpaperService.this;
}
} else {
if (modParlante) {
// TextureManager.getInstance().removeTexture("texture");
// loadfire();

world.removeObject(cube);
world.removeObject(cube2);
world.removeObject(cube3);
world.removeObject(cube4);
world.removeObject(cube5);

cube = null;
cube2 = null;
cube3 = null;
cube4 = null;
cube5 = null;

try {
if (mNumParlante == 1)
cargarParlante1();
else if (mNumParlante == 2)
cargarParlante2();
else if (mNumParlante == 3)
cargarParlante3();
} catch (IOException e) {
e.printStackTrace();
}
world.addObject(cube);

if(mDesHabParSec){
world.addObject(cube2);
world.addObject(cube3);
world.addObject(cube4);
world.addObject(cube5);
}

//DESHABILITA CAMBIO DE CONFIGURACION
modParlante = false;
}
}

}





load model is as follows:
case is 1
cube = Object3D.mergeAll(Loader.load3DS(getResources().getAssets().open("name1.3ds"), 1));

case is 2
cube = Object3D.mergeAll(Loader.load3DS(getResources().getAssets().open("name2.3ds"), 1));

EgonOlsen

Honestly, i've no idea what you are trying to say...

Is this another problem or just another way of describing it?

canoide1592

Quote from: EgonOlsen on February 22, 2014, 08:05:20 PM
Honestly, i've no idea what you are trying to say...

Is this another problem or just another way of describing it?

I can change a Model 1 Model 2 for occupying the same variable? ie load the model 2 in the variable of model 1

EgonOlsen

Yes, why not...i still don't get the question.

However, you are doing a lot of stuff in onSurfaceChanged that's actually not needed. You can load and create everything at first startup and use it from there. You just have to make sure that a new Activity instance can use the data created by the former one. The HelloWorld example show one way of doing this. All this removing and reloading actually isn't needed.

canoide1592

#8
Quote from: EgonOlsen on February 22, 2014, 08:48:10 PM
Yes, why not...i still don't get the question.

However, you are doing a lot of stuff in onSurfaceChanged that's actually not needed. You can load and create everything at first startup and use it from there. You just have to make sure that a new Activity instance can use the data created by the former one. The HelloWorld example show one way of doing this. All this removing and reloading actually isn't needed.

As I can do to change one model to another?

In preference I have 3 models to choose and change between these 3 models. Depending upon which selected.

I'm doing a live Wallpaper

you could pass the project and see if you can see the problem? which increases the memory consumption?

EgonOlsen

I already gave you some tips on how to tackle your memory problem. I got no feedback from you if you tried those and if they helped. You also haven't answered the question if this memory usage actually causes a real problem or if you just want to make it use less memory!? Sorry, but i can't be bothered to debug some wallpaper code...

sushobhit

To minimise memory consumption and lesser program crashes I would recommend you to do least coding in the drawFrame() function.....

Also (this one I got from the Physics Example)
(Although doesnt apply to you as your's is not a Activity)

int pid = android.os.Process.myPid();
android.os.Process.killprocess(pid);
Should do this on destroy() & if possible in onPause() :-*

sushobhit