处理类对象的 MATLAB 弱引用

发布于 2024-10-07 23:36:52 字数 574 浏览 0 评论 0原文

在考虑 MATLAB 中基于句柄类的 ORM 的可能性时,出现了缓存实例的问题。我无法立即想到一种方法来制作弱引用或弱映射,尽管我猜测可以通过事件监听器来设计一些东西。有什么想法吗?

更多信息

在 MATLAB 中,句柄类(与值类相对)具有引用语义。 MATLAB 附带的一个示例是 containers.Map 类。如果实例化一个对象并将其传递给函数,则该函数对对象所做的任何修改都将通过原始引用可见。也就是说,它的工作方式类似于 Java 或 Python 对象引用。

与 Java 和 Python 一样,MATLAB 以一种或另一种方式跟踪有多少事物正在引用句柄类的每个对象。当不再有任何对象时,MATLAB 知道可以安全地删除该对象。

弱引用是指引用对象但不计为垃圾回收目的引用的引用。因此,如果对该对象仅存的引用较弱,则可以将其丢弃。通常,可以向弱引用提供事件或回调 - 当对象被丢弃时,将通知对其的弱引用,从而允许运行清理代码。

例如,弱值映射类似于法线映射,只不过值(而不是键)被实现为弱引用。弱映射类可以在每个弱引用上安排回调或事件,以便当删除引用的对象时,映射中的键/值条目将被删除,从而保持映射的整洁。

While thinking about the possibility of a handle class based ORM in MATLAB, the issue of caching instances came up. I could not immediately think of a way to make weak references or a weak map, though I'm guessing that something could be contrived with event listeners. Any ideas?

More Info

In MATLAB, a handle class (as opposed to a value class) has reference semantics. An example included with MATLAB is the containers.Map class. If you instantiate one and pass it to a function, any modifications the function makes to the object will be visible via the original reference. That is, it works like a Java or Python object reference.

Like Java and Python, MATLAB keeps track in one way or another of how many things are referencing each object of a handle class. When there aren't any more, MATLAB knows it is safe to delete the object.

A weak reference is one that refers to the object but does not count as a reference for purposes of garbage collection. So if the only remaining references to the object are weak, then it can be thrown away. Generally an event or callback can be supplied to the weak reference - when the object is thrown away, the weak references to it will be notified, allowing cleanup code to run.

For instance, a weak value map is like a normal map, except that the values (as opposed to the keys) are implemented as weak references. The weak map class can arrange a callback or event on each of these weak references so that when the referenced object is deleted, the key/value entry in the map is removed, keeping the map nice and tidy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没企图 2024-10-14 23:36:52

这些特殊的引用类型实际上是语言级别的功能,需要 VM 和 GC 来完成。尝试在用户代码中实现它可能会以泪水告终,特别是如果您依赖于未记录的行为。 (很抱歉陷入困境。)

有几种方法可以做类似的事情。这些只是想法,并非认可;我实际上还没有做过。

也许您可以使用嵌入在 Matlab 中的 JVM 中的真实 Java 弱引用映射来缓存昂贵的计算结果,而不是缓存 Matlab 对象实例本身。如果您可以相对快速地将 Matlab 值与 Java 值相互转换,那么这可能是一个胜利。如果它是相对平坦的数值数据,像 double[] 或 double[][] 这样的原语可以使用 Matlab 的隐式转换快速转换。

或者,您可以在 Matlab 级别创建一个常规的 LRU 对象缓存(可能使用由哈希码作为键控的Containers.Map),当添加新对象时,它会显式删除其中的对象。要么直接使用它,要么向对象添加 onCleanup() 行为,让对象自动将自身的副本添加到固定大小的全局“最近删除的对象”LRU 缓存中,以外部有意义的 id 为键,并在中标记实例因此,当它们因缓存过期而被删除时,您的 onCleanup() 方法不会尝试重新添加它们。然后,您可以使用工厂方法或其他查找方法从缓存中“复活”实例,而不是以昂贵的方式构建全新的实例。不过,这听起来工作量很大,而且确实不是 Matlab 惯用的做法。

These special reference types are really a language-level feature, something you need the VM and GC to do. Trying to implement it in user code will likely end in tears, especially if you lean on undocumented behavior. (Sorry to be a stick in the mud.)

There's a couple ways you could do something similar. These are just ideas, not endorsements; I haven't actually done them.

Perhaps instead of caching Matlab object instances per se, you could cache expensive computational results using a real Java weak ref map in the JVM embedded inside Matlab. If you can convert your Matlab values to and from Java relatively quickly, this could be a win. If it's relatively flat numeric data, primitives like double[] or double[][] convert quickly using Matlab's implicit conversion.

Or you could make a regular LRU object cache in the Matlab level (maybe using a containers.Map keyed by hashcodes) that explicitly removes the objects inside it when new ones are added. Either use it directly, or add an onCleanup() behavior to your objects that has them automatically add a copy of themselves to a global "recently deleted objects" LRU cache of fixed size, keyed by an externally meaningful id, and mark the instances in the cache so your onCleanup() method doesn't try to re-add them when they're deleted due to expiration from the cache. Then you could have a factory method or other lookup method "resurrect" instances from the cache instead of constructing brand new ones the expensive way. This sounds like a lot of work, though, and really not idiomatic Matlab.

§普罗旺斯的薰衣草 2024-10-14 23:36:52

这不是你问题的答案,只是我的 2 美分。

弱引用是垃圾收集器的一个特性。在 Java 和 .NET 中,当内存压力很大且因此不确定时,就会调用垃圾收集器。

这篇 MATLAB 摘要文章 说 MATLAB 是不使用(不确定的)垃圾收集器。在 MATLAB 中,每次弹出堆栈时(即离开每个函数时),引用都会从内存中(确定性地)删除。

因此,我不认为弱引用属于 MATLAB 引用处理概念。但 MATLAB 始终有大量未记录的功能,因此我不能排除它被隐藏在某个地方。

这篇文章中,我询问了有关 MATLAB 垃圾收集器实现的问题,但没有得到真正的答案。一位 MathWorks 员工没有回答我的问题,而是指责我试图构建 Python 与 MATLAB 的争论。另一位 MathWorks 员工写了一些看似合理但实质上是巧妙的欺骗——故意分散我对问题的注意力。最好的答案是:

如果你问这个问题,那么 MATLAB
不适合您的语言!

This is not an answer to your question but just my 2 cents.

Weak reference is a feature of garbage collector. In Java and .NET garbage collector is being called when the pressure on memory is high and is therefore indeterministic.

This MATLAB Digest post says that MATLAB is not using a (indeterministic) garbage collector. In MATLAB references are being deleted from memory (deterministically) on each stack pop i.e. on leaving each function.

Thus I do not think that weak references belongs to the MATLAB reference handling concept. But MATLAB has always had tons of undocumented features so I can not exclude that it is buried somewhere.

In this SO post I asked about MATLAB garbage collector implementation and got no real answer. One MathWorks stuff member instead of answering my question has accused me of trying to construct a Python vs. MATLAB argument. Another MathWorks stuff member wrote something looking reasonable but in substance a clever deception - purposeful distraction from the problem I asked about. And the best answer has been:

if you ask this question then MATLAB
is not the right language for you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文