我正在处理一些释放问题,也许还有无法弄清楚的强引用或循环引用。我有 3 个UIView实例化,如下所示:
ViewController我在故事板中添加了一个主要内容,UIView并且在类中UIView有一个出口,例如:weak
class ViewController : UIViewController {
//MARK: - outlets
@IBOutlet weak var firstView: FirstUiview!
}
Run Code Online (Sandbox Code Playgroud)
第二个UIView作为子视图以编程方式添加到第一个视图,如下所示:
class FirstUiview : UIView {
//creating an instance of secondUiView
lazy var mySecondView: SecondViewClass = {
let dv = SecondViewClass()
dv.backgroundColor = UIColor.red
return dv
}()
//sometime later by clicking on a button
self.addSubview(mySecondView)
//a button will be tapped to remove mySecondView;
//later will be called at some point upon tapping: …Run Code Online (Sandbox Code Playgroud) 我的应用程序相机预览记录应用程序。我ArrayList在录制相机预览期间使用 , 。
ArrayList在全局变量上声明
private ArrayList<OutputInputPair> pairs = new ArrayList<OutputInput>();
当我记录停止按钮单击时,执行stop()方法
@Override
public void stop() {
pairs.clear();
pairs = null;
stopped = true;
}
Run Code Online (Sandbox Code Playgroud)
所以,如果我继续录制而不单击录制停止按钮。发生大量内存泄漏。
所以,我想用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 …Run Code Online (Sandbox Code Playgroud) 我认为主要目的__slots__是通过允许显式指定属性来节省内存使用量,而不是允许__dict__在实例上动态分配属性。所以我以某种方式理解为什么__dict__在使用时默认删除__slots__。但为什么它同时__weakref__默认删除呢?
参考: https: //docs.python.org/3/reference/datamodel.html#slots
当它们因为最后一个强引用消失而被清除时,如果这发生在另一个线程上,你会遇到问题吗?如果是这样,使用 atomic 是否正确?既然我相信 Swift 没有原子,那么那里的线程安全弱属性需要什么?
我在 macOS 应用程序中使用 Swift,Xcode 是 12.5.1。想象一下,我有以下代码:
func performAsyncTask(completion: { [weak self] (error: Error?) in
self?.someProperty = 0.0
self?.someOtherProperty = 0.0
// Other similar statements follow
})
Run Code Online (Sandbox Code Playgroud)
将其更改为:
func performAsyncTask(completion: { [weak self] (error: Error?) in
guard let self = self else { return }
self.someProperty = 0.0
self.someOtherProperty = 0.0
// Other similar statements follow
})
Run Code Online (Sandbox Code Playgroud)
我相信第一个实现更好,因为 self 可能在语句之间变为 nil ,因此在开始时展开可能不那么干净。我希望专家能给我带来正确的方向。感谢您的关注。
我一直很难处理烧瓶中的会话。自从我在本地环境中管理应用程序以来,一切都运行良好,包括烧瓶会话。但是当我已经在渲染中托管它时,我总是在每条路线中收到此错误。
[55] [ERROR] Error handling request /valle-de-guadalupe
Traceback (most recent call last):
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/opt/render/project/src/app_folder/routes/public.py", line 35, in valle_de_guadalupe
return render_template("public/cities/valle_guadalupe.html")
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 147, in render_template
return _render(app, template, context)
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/templating.py", line 128, in _render
app.update_template_context(context)
File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 994, in update_template_context
context.update(func())
File …Run Code Online (Sandbox Code Playgroud) 为什么我的weakRef.Target在第二次射击时仍然存在?
这可能是一个错误吗?如果不是,错误在哪里?
结果:
weakRef.Target is alive = True, expected true because inst keep a hold on SomeClass.
weakRef.Target is alive = True, expected false, because there is no more ref on SomeClass.
Run Code Online (Sandbox Code Playgroud)
代码:
public static class DelegateKeeper
{
private static ConditionalWeakTable<object, Action> cwtAction = new ConditionalWeakTable<object, Action>();
public static void KeepAlive(Action action) => cwtAction.Add(action.Target, action);
}
public class SomeClass
{
public void DoSomething() { }
}
public static class GcHelper
{
public static void Collect()
{
// OK surely overkill …Run Code Online (Sandbox Code Playgroud) 我是否需要检查弱自我是否在块中为零?
我创建了weakSelf指针,如:
__weak typeof(self) weakSelf = self;
Run Code Online (Sandbox Code Playgroud)
在我做的块的开头
if(!weakSelf){return;}
Run Code Online (Sandbox Code Playgroud)
这是不必要的吗?或者它取决于我是否正确编码其余部分,以便当自我死亡时,其他人也会死亡?
1)实际上问题出在代码中.持有对正在运行的线程的引用会阻止对象符合垃圾回收的条件吗?
class SomeClass {
Thread mThread = new Thread(new MyRunnable());
public SomeClass() {
mThread.start();
}
public static void main(String[] args) {
SomeClass sc = new SomeClass();
sc = null;
// Will the sc be eligible to gc here?
...
}
private static class MyRunnable implements Runnable {
@Override
public void run() {
while (true) {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
2)如果我们使用WeakReference而不是Thread,它是否符合条件?
更新
更新了代码以强调最困扰我的问题.
我想要了解的是,运行线程的实例是否阻止对象被收集?