My app camera preview record app.
I use ArrayList
, during record camera preview.
ArrayList
declared on global variable
private ArrayList<OutputInputPair> pairs = new ArrayList<OutputInput>();
and when I record stop button click, execute stop()
method
@Override
public void stop() {
pairs.clear();
pairs = null;
stopped = true;
}
so, If I continue recording without click record stop button. occur a lot of memory leak.
so, I want use WeakReference
I try this
//private ArrayList<OutputInputPair> pairs = new ArrayList<OutputInputPair();
private ArrayList<WeakReference<OutputInputPair>> pairs = new ArrayList<WeakReference<OutputInputPair>>(); //global variable
@Override
public void add(OutputInputPair pair) {
//pairs.add(pair);
pairs.add(new WeakReference<OutputInputPair>(pair));
}
@Override
public void stop() {
pairs.clear();
pairs = null;
stopped = true;
}
@Override
public void process() { //record method
//for (OutputInputPair pair : pairs) {
for (WeakReference<OutputInputPair> pair = pairs) {
pair.output.fillCommandQueues(); //output is cannot resolve symbol message
pair.input.fillCommandQueues(); //input is cannot resolve symbol message
}
while (!stopped) { //when user click stop button, stopped = true.
//for (OutputInputPair pair : pairs) {
for (WeakReference<OutputInputPair> pair : pairs) {
recording(pair); //start recording
}
}
}
public interface IOutputRaw { //IInputRaw class same code.
void fillCommandQueues();
}
I think How to avoid memory rick, use WeakReference
is right?
How to fix cannot resolve symbol message use weakreference?
thanks.
public class OutputInputPair {
public IOutputRaw output;
public IInputRaw input;
public OutputInputPair(IOutputRaw output, IInputRaw input) {
this.output = output;
this.input = input;
}
}
I don't know much of WeakReference
. But you should use the get()
method to get the actual reference.
Use:
if(pair == null) continue;
OutputInputPair actualPair = pair.get();
if(actualPair == null) continue;
actualPair.output.fillCommandQueues();
actualPair.input.fillCommandQueues();
instead of:
pair.output.fillCommandQueues();
pair.input.fillCommandQueues();