question regarding call sites and garbage collection

Rémi Forax forax at univ-mlv.fr
Wed Mar 16 09:26:31 PDT 2011


On 03/16/2011 11:36 AM, Jochen Theodorou wrote:
> Am 15.03.2011 23:41, schrieb Rémi Forax:
> [...]
>>> If the callsite is inlined, won't
>>> that mean that then we have those types hard referenced as well and that
>>> I cannot do anything against that?
>> A callsite can be inlined more than once.
>> So the reference to the mh can not be dropt
> I ask because I was thinking of a certain artificial case
>
> let us assume I have a class that does this:
>
> Script1:
>
> Object foo(x) {
>     return x
> }
> Object bar(x) {
>     x.foo(this) // done by call site logic using MethodHandle
> }
>
> Then, if I understood correctly, whatever the class of x is, the class
> will be referenced from this call site and has to live as long as this
> class here does.

The class may be referenced or not, it depends how your compiler
and your inlining cache are implemented.
Let suppose the compiler doesn't do any type inference,
in that case,
    x.foo(this)
is translated to
    invokedynamic [aBSM] foo(Object,Object)V
so there is no reference to X here.

Now when bar() is called with an instance of X.
Let supoose you use a guardWithTest and store a method handle to foo.
foo is compiled Object foo(Object).
so there is no reference to X here.
But perhaps your test (the GWT test) is a X.isInstance(), then you have 
a reference to X
from the callsite (you have inserted X in the test method handle).
Your test can also be a test on the metaclass or an id, in that case
you have no reference to X.

> Now assume I will do the following:
>
> Object lastScript = null
> bigNumber.times {
>       MyLoader cl = new CustomClassLoaderOfSomeKind()
>       def scriptn = cl.defineClass(...).newInstance()
>       scriptn.bar(lastScript==null?scriptn:lastScript)
>       lastScript = scriptn
> }
>
> The result, should I have understood correctly, will be, that each
> scriptn will cause the former scriptn (lastScript) to be referenced in a
> way that prefents that from being garbage collected, even though scriptn
> does not reference lastScript directly and there will be no reference to
> the class loader as well. So in theory the last script could be garbage
> collected but the MethodHandles prevent that from happening.

It's not the method handles by itself but how you implement your 
inlining cache.

> bye Jochen

Rémi




More information about the mlvm-dev mailing list