Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Aspects holding onto weak references?


Mindaugas,

Eric is right. System.gc() is just a hint to the JVM (which it can choose to ignore) and System.runFinalization() will have no effect on weak references. If you create a few more objects your aspect will work:

package test;

public class Test {
       
    public static void main(String[] args) {

            for (int i = 0; i < 100; i++) {
            Test t = new Test();
            }

            for (int i = 0; i < 10; i++) {

                    System.gc(); // <-- this is where normally, the ref clearing thread will print out "ref cleared"
                    System.runFinalization();
            }

    }
}



Matthew Webster
AOSD Project
Java Technology Centre, MP146
IBM Hursley Park, Winchester,  SO21 2JN, England
Telephone: +44 196 2816139 (external) 246139 (internal)
Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
http://w3.hursley.ibm.com/~websterm/



"Eric Bodden" <eric.bodden@xxxxxxxxxxxxxx>
Sent by: aspectj-users-bounces@xxxxxxxxxxx

07/11/2006 20:30

Please respond to
aspectj-users@xxxxxxxxxxx

To
<aspectj-users@xxxxxxxxxxx>
cc
Subject
RE: [aspectj-users] Aspects holding onto weak references?





Hi.

I can assure you that certainly the aspect does not construct a strong
reference. If this was true, a lot of AspectJ programs would have failed
before.

I think the problem might rather be your methodology. System.gc() does
not actually have to perform a GC, neither does System.runFinalization()
have to run finalization.

Eric


> -----Original Message-----
> From: aspectj-users-bounces@xxxxxxxxxxx [mailto:aspectj-users-
> bounces@xxxxxxxxxxx] On Behalf Of Mindaugas Idzelis
> Sent: Tuesday, November 07, 2006 11:17 AM
> To: aspectj-users@xxxxxxxxxxx
> Subject: [aspectj-users] Aspects holding onto weak references?
>
>
> I am trying to design an aspect where I grab the stack trace when
> objects are constructed. I would like to display them when the objects
> are destroyed. I would like to do this on the call-side since the
> weaver may not have the source to the target object.
>
> I'm finding that somewhere the aspect is holding a hard reference onto
> the target object. When I run a similar solution without using
aspects,
> it works as expected. Any thoughts on how/why the aspect would prevent
> the collection of the target object?
>
>
> Here's an example aspect to illustrate the problem. The problem is
that
> the "Ref cleared" is never printed out.
>
> public aspect WeakRefProblem {
>
>         public static IdentityHashMap stacks = new IdentityHashMap();
>
>         public static ReferenceQueue q = new ReferenceQueue();
>
>         static {
>                 new Thread("ref clearing thread") {
>                         public void run() {
>                                 while (true) {
>                                         try {
>                                                 WeakReference r =
> (WeakReference) q.remove();
>                                                 Test list = (Test)
> r.get();
>
System.out.println("Ref
> cleared");
>                                                 Exception e =
> (Exception) stacks.get(r);
>                                                 e.printStackTrace();
>                                         } catch (InterruptedException
> e) {
>                                                 e.printStackTrace();
>                                         }
>
>                                 }
>                         }
>                 }.start();
>         }
>
>         after() returning(Test list) : call(Test+.new(..)) {
>                 WeakReference ref = new WeakReference(list, q);
>                 stacks.put(ref, new Exception());
>         }
>
> }
>
> test program
>
> public class Test {
>         public static void main(String[] args) {
>
>                 Test t = new Test();
>
>                 t = null;
>                 for (int i = 0; i < 10; i++) {
>
>                         System.gc(); // <-- this is where normally,
the
> ref clearing thread will print out "ref cleared"
>                         System.runFinalization();
>                 }
>
>         }
> }
>
>
> Thanks,
>
> Mindaugas Idzelis
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top