8

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.

Mikhail Poda
  • 5,742
  • 3
  • 39
  • 52
kenm
  • 23,127
  • 2
  • 43
  • 62
  • 1
    Very good question but too advanced, in my opinion, even for the MATLAB power-users. You have to explain what ORM and weak listeners are, else people will have strange theories (like here http://mathforum.org/kb/thread.jspa?threadID=1911007&messageID=6644136) – Mikhail Poda Dec 16 '10 at 18:07
  • @Mikhail - The ORM concept isn't really relevant to the problem, but I'll at least explain weak references. – kenm Dec 16 '10 at 20:25
  • 2
    At least Java, if I remember correctly has 3 kinds of week references - weak, soft and phantom. Which one do you prefer? – Mikhail Poda Dec 16 '10 at 20:44
  • @Mikhail, for my purposes weak would be sufficient but soft would be more useful. Querying MATLAB's memory manager seems unlikely though (the `memory` command isn't even available on my platform), so I'm aiming for weak. – kenm Dec 16 '10 at 20:53
  • I think that you should mirror this question on http://www.mathworks.com/matlabcentral/newsreader/, it is more probable that somebody from MathWorks will notice it. I think only they can answer it. – Mikhail Poda Dec 16 '10 at 21:03
  • Asked here on Answers which at this time also has no answer: https://www.mathworks.com/matlabcentral/answers/161781-implementing-a-weak-reference-in-matlab – Jimbo Mar 25 '20 at 18:17

2 Answers2

3

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!

Community
  • 1
  • 1
Mikhail Poda
  • 5,742
  • 3
  • 39
  • 52
  • That sounds like my typical interaction with MathWorks staff - they either don't understand the question or tell me that it isn't possible. – kenm Dec 17 '10 at 15:43
  • Oh, and CPython does deterministic deletion via reference counts, and they have weak references, so I think it is something MathWorks could implement if they cared. (Of course, CPython also has a GC, but that's for killing cycles). – kenm Dec 17 '10 at 15:47
3

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.

Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
  • I agree about using undocumented behavior. I did try scooping a handle object into a Java reference using the (undocumented) JMI, but its destructor did not execute reliably in that situation. – kenm Dec 17 '10 at 21:49
  • Oh, and I do have an LRU already set up for something else, but the characteristics of this particular problem suggested a weakref cache. I just wanted to know if the weakref was possible. – kenm Dec 17 '10 at 21:53