我的应用相机预览记录应用。
我用 ArrayList
, 在录制相机预览期间。
ArrayList
在全局变量上声明
private ArrayList<OutputInputPair> pairs = new ArrayList<OutputInput>();
当我记录停止按钮点击时,执行stop()
方法
@Override
public void stop() {
pairs.clear();
pairs = null;
stopped = true;
}
所以,如果我继续录制而不单击录制停止按钮。 发生大量内存泄漏。
所以,我想使用 WeakReference
我试试这个
//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();
}
我认为如何避免内存力下降,使用WeakReference
是吗?
如何修复无法解析符号消息使用弱引用?
谢谢。
public class OutputInputPair {
public IOutputRaw output;
public IInputRaw input;
public OutputInputPair(IOutputRaw output, IInputRaw input) {
this.output = output;
this.input = input;
}
}
最佳答案
我对 WeakReference
了解不多。但是您应该使用 get()
方法来获取实际引用。
使用:
if(pair == null) continue;
OutputInputPair actualPair = pair.get();
if(actualPair == null) continue;
actualPair.output.fillCommandQueues();
actualPair.input.fillCommandQueues();
代替:
pair.output.fillCommandQueues();
pair.input.fillCommandQueues();
关于java - 当我使用 WeakReference 时,无法解析 android 上的符号消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46944666/