Trace what prevents an object from being GC'd?

Trace what prevents an object from being GC'd?

Postby thomthom » Fri Sep 16, 2011 2:22 pm

Is there a way to trace back the references of an object in some manner? Finding the cause to why an object isn't garbage collected.

I've used ObjectSpace to inspect the count, where I've noticed some objects aren't GC'd. And after trying to set all references I know of to nil - they still aren't GC'd.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: Trace what prevents an object from being GC'd?

Postby tbd » Fri Sep 16, 2011 4:04 pm

several solutions:
* patch ruby vm - https://gist.github.com/73674
* memproof gem - http://timetobleed.com/memprof-a-ruby-l ... -profiler/ (Linux x64 only)
* ripping the internals via debugger ;) - http://www.coffeepowered.net/2010/08/23 ... b-round-2/

does it eat a lot memory ?
0
SketchUp Ruby Consultant | Podium 1.x developer
http://plugins.ro
User avatar
tbd 
 

Re: Trace what prevents an object from being GC'd?

Postby thomthom » Fri Sep 16, 2011 4:08 pm

TBD wrote:does it eat a lot memory ?

theee... I don't have an overview of that. ...I know... don't optimize what doesn't need to be, but it bothers me that I don't know why.

It's a subclasses WebDialog, where a WebDialog will normally GC, my subclassed version doesn't. So everytime I create one, it lingers about. (how much memory usage, I'd expect depends on the HTML content.)

(another odd thing about WebDialogs, when I create one, a normal one, ObjectSpace's count increased by two... :?
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: Trace what prevents an object from being GC'd?

Postby Dan Rathbun » Fri Sep 16, 2011 5:21 pm

thomthom wrote:(another odd thing about WebDialogs, when I create one, a normal one, ObjectSpace's count increased by two... :?

I remember AdamB posting about this in another thread last year (or so,)....

.. anyway. The "hidden" reference is held by the C++ as the object is a MFC window object (on PC,) and in ObjectiveC (on Mac,) as an NSWindow object.
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Trace what prevents an object from being GC'd?

    Postby Dan Rathbun » Fri Sep 16, 2011 5:51 pm

    It does occur to me that we do not have a call to tell the Sketchup C++ engine that we are done with a WebDialog instance and want it "disposed of".

    We would need an .dispose() instance method for the UI::WebDialog class, that would tell the C++ engine to get rid of it's reference (and then sets the receiver on the Ruby-side to nil [if that's possible.])

    Then you can get rid of your class definition using remove_const(:MyWebDialogSubclass)
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: Trace what prevents an object from being GC'd?

      Postby thomthom » Fri Sep 16, 2011 6:29 pm

      The thing is, a normal WebDialog instance will be GC'd if you clear all references to it.

      But there is something I have done in my subclass that prevents this - and I'm not finding out why.. :(
      0
      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund
      User avatar
      thomthom 
      PluginStore Author
      PluginStore Author
       

      Re: Trace what prevents an object from being GC'd?

      Postby Dan Rathbun » Fri Sep 16, 2011 6:34 pm

      thomthom wrote:The thing is, a normal WebDialog instance will be GC'd if you clear all references to it.

      Sure.. as I remember Adam's post .. I thot he showed they were not GC'd.. but is was awhile back.

      It could be nothing more exotic than that Google did not account for us subclassing UI::WebDialog, similar to the way some API methods only accept exact classes and not any subclasses.
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        Re: Trace what prevents an object from being GC'd?

        Postby Dan Rathbun » Fri Sep 16, 2011 6:36 pm

        Did you dispose of all Locals, Constants, instance vars, and perhaps maybe even Procs that are within your subclass?
        0
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: Trace what prevents an object from being GC'd?

          Postby thomthom » Fri Sep 16, 2011 6:37 pm

          Maybe... I was planning on trying a simple subclass - without all the stuff I have in mine. Just to see if that makes the differences.

          You could test the GC of a WebDialog if you like - so we can compare results.

          I did a count of the existing WebDialogs, then created a new one. Made a new count ( +2 in my results ) then I set my references to the WD to nil and invoke GC.start. The count then went down by 2. Confirm?
          0
          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund
          User avatar
          thomthom 
          PluginStore Author
          PluginStore Author
           

          Re: Trace what prevents an object from being GC'd?

          Postby thomthom » Fri Sep 16, 2011 6:38 pm

          Dan Rathbun wrote:Did you dispose of all Locals, Constants, instance vars, and perhaps maybe even Procs that are within your subclass?


          I set all local variables to nil, nothing to the constants - just strings.
          0
          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund
          User avatar
          thomthom 
          PluginStore Author
          PluginStore Author
           

          Re: Trace what prevents an object from being GC'd?

          Postby Dan Rathbun » Fri Sep 16, 2011 7:41 pm

          thomthom wrote:You could test the GC of a WebDialog if you like - so we can compare results. ... Confirm?

          OK, here:
          Code: Select all
          dlg = UI::WebDialog.new
          >> #<UI::WebDialog:0x61dc79c>
          ObjectSpace.each_object(UI::WebDialog) {}
          >> 2
          dlg=nil
          >> nil
          ObjectSpace.each_object(UI::WebDialog) {}
          >> 2
          GC.start
          >> nil
          ObjectSpace.each_object(UI::WebDialog) {}
          >> 0
          MyDialog = Class.new(UI::WebDialog)
          >> MyDialog
          MyDialog.superclass
          >> UI::WebDialog


          dlg=MyDialog.new
          >> #<MyDialog:0x6071ed4>
          ObjectSpace.each_object(UI::WebDialog) {}
          >> 2
          ObjectSpace.each_object(MyDialog) {}
          >> 2
          dlg=nil
          >> nil
          ObjectSpace.each_object(MyDialog) {}
          >> 2
          ObjectSpace.each_object(UI::WebDialog) {}
          >> 2
          GC.start
          >> nil
          ObjectSpace.each_object(MyDialog) {}
          >> 0
          ObjectSpace.each_object(UI::WebDialog) {}
          >> 0
          0
            I'm not here much anymore. But a PM will fire email notifications.
            User avatar
            Dan Rathbun 
            PluginStore Author
            PluginStore Author
             

            Re: Trace what prevents an object from being GC'd?

            Postby Dan Rathbun » Fri Sep 16, 2011 7:44 pm

            But I did not show() the dialog in the previous test, so:
            Code: Select all
            dlg=MyDialog.new('Test')
            >> #<MyDialog:0x61d2f30>
            dlg.show
            >> true
            # empty dialog with title 'Test' is displayed,
            # .. and I close it manually.
            ObjectSpace.each_object(UI::WebDialog) {}
            >> 2
            ObjectSpace.each_object(MyDialog) {}
            >> 2
            dlg=nil
            >> nil
            ObjectSpace.each_object(MyDialog) {}
            >> 2
            ObjectSpace.each_object(UI::WebDialog) {}
            >> 2
            GC.start
            >> nil
            ObjectSpace.each_object(UI::WebDialog) {}
            >> 0
            ObjectSpace.each_object(MyDialog) {}
            >> 0
            0
              I'm not here much anymore. But a PM will fire email notifications.
              User avatar
              Dan Rathbun 
              PluginStore Author
              PluginStore Author
               

              Re: Trace what prevents an object from being GC'd?

              Postby Dan Rathbun » Fri Sep 16, 2011 7:50 pm

              Are you testing on PC (v1.8.6-p287) or Mac (v1.8.5-p0) ??

              I guess I should say that I'm running:
              RUBY_VERSION
              1.8.7
              RUBY_PATCHLEVEL
              299


              .. get I set it for a test and forgot to set it back.
              0
                I'm not here much anymore. But a PM will fire email notifications.
                User avatar
                Dan Rathbun 
                PluginStore Author
                PluginStore Author
                 

                Re: Trace what prevents an object from being GC'd?

                Postby Dan Rathbun » Fri Sep 16, 2011 11:34 pm

                @ThomThom...

                look at this example:
                https://gist.github.com/355820

                A the bottom he determines that there is a finalizer that is holding a ref to the instance, and therefor the reason why the instance is not getting GC'd.

                I know you were trying to use a finalizer to cleanup a temp file.. likely that's the culprit.
                0
                  I'm not here much anymore. But a PM will fire email notifications.
                  User avatar
                  Dan Rathbun 
                  PluginStore Author
                  PluginStore Author
                   

                  Re: Trace what prevents an object from being GC'd?

                  Postby thomthom » Fri Sep 16, 2011 11:59 pm

                  Dan Rathbun wrote:@ThomThom...

                  look at this example:
                  https://gist.github.com/355820

                  A the bottom he determines that there is a finalizer that is holding a ref to the instance, and therefor the reason why the instance is not getting GC'd.

                  I know you were trying to use a finalizer to cleanup a temp file.. likely that's the culprit.

                  Not in this case. I'm testing on SU8, default Ruby. I've used finalizer, but it only triggers under OSX. And I've read the gotchas about finalizers not GC'ing - and implemented it so it doesn't hold a reference.

                  However, I think I'm somehow creating a reference somewhere... I just can't track it down...
                  0
                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund
                  User avatar
                  thomthom 
                  PluginStore Author
                  PluginStore Author
                   

                  Re: Trace what prevents an object from being GC'd?

                  Postby thomthom » Sat Sep 17, 2011 5:43 pm

                  It seems to be due to using self.add_action_callback within the initialize method of the subclass.

                  Code: Select all
                  class WC < UI::WebDialog; def initialize(*args); super; self.add_action_callback('test') { |diag,param| puts diag, param };end;end;
                  nil

                  ObjectSpace
                  .each_object(WC){}
                  0

                  x
                  =WC.new 'test'
                  #<WC:0x18dc1000>

                  ObjectSpace.each_object(WC){}
                  2

                  x
                  =nil
                  nil

                  GC
                  .start
                  nil

                  ObjectSpace
                  .each_object(WC){}
                  2



                  When I use add_action_callback outside initialize it does get GC'd.
                  Code: Select all
                  ObjectSpace.each_object(UI::WebDialog){}
                  2

                  wd
                  =UI::WebDialog.new 'test'
                  #<UI::WebDialog:0x18f1a2a8>

                  ObjectSpace.each_object(UI::WebDialog){}
                  4

                  wd
                  .add_action_callback('test') { |diag,param| puts diag, param }
                  true

                  wd 
                  = nil
                  nil

                  GC
                  .start
                  nil

                  ObjectSpace
                  .each_object(UI::WebDialog){}
                  2


                  I don't understand why...
                  0
                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund
                  User avatar
                  thomthom 
                  PluginStore Author
                  PluginStore Author
                   

                  Re: Trace what prevents an object from being GC'd?

                  Postby Dan Rathbun » Sat Sep 17, 2011 11:18 pm

                  thomthom wrote:It seems to be due to using self.add_action_callback within the initialize method of the subclass.

                  I don't understand why...


                  Oh.. that's an easy one: Because self IS a reference to THE instance.
                  So YOUR telling Ruby to keep 'literal' reference within the code.

                  You dont need that reference by the way.. because the initialize method ALWAYS gets executed WITHIN the newly created instance, just after the class constructor (usually new,) creates it.

                  So just remove the "self." from in front of ALL instance method calls made from within ANY of the object's instance methods.
                  0
                    I'm not here much anymore. But a PM will fire email notifications.
                    User avatar
                    Dan Rathbun 
                    PluginStore Author
                    PluginStore Author
                     

                    Re: Trace what prevents an object from being GC'd?

                    Postby Chris Fullmer » Sat Sep 17, 2011 11:33 pm

                    Interesting, :thumb:
                    0
                    Lately you've been tan, suspicious for the winter.
                    All my Plugins I've written
                    User avatar
                    Chris Fullmer 
                    SketchUp Team
                    SketchUp Team
                     

                    Re: Trace what prevents an object from being GC'd?

                    Postby thomthom » Sun Sep 18, 2011 12:31 am

                    I had a suspicion that might be it, and it's what I had planned to try next. But I still don't understand why. As self.add_action_callback is just calling a method, never is self being stored in any reference... :?:
                    0
                    Thomas Thomassen — SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund
                    User avatar
                    thomthom 
                    PluginStore Author
                    PluginStore Author
                     

                    Re: Trace what prevents an object from being GC'd?

                    Postby thomthom » Sun Sep 18, 2011 12:36 am

                    Removing self. from add_action_callback made no difference.
                    0
                    Thomas Thomassen — SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund
                    User avatar
                    thomthom 
                    PluginStore Author
                    PluginStore Author
                     

                    Re: Trace what prevents an object from being GC'd?

                    Postby thomthom » Sun Sep 18, 2011 12:46 am

                    I finally nailed it. I created the procs for add_action_callback as class constants instead of creating blocks within the initialize method and that allowed the WebDialog to be GC'd.
                    0
                    Thomas Thomassen — SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund
                    User avatar
                    thomthom 
                    PluginStore Author
                    PluginStore Author
                     

                    Re: Trace what prevents an object from being GC'd?

                    Postby Dan Rathbun » Sun Sep 18, 2011 2:52 am

                      Dan Rathbun in the 8th post of this topic wrote:
                      Did you dispose of all Locals, Constants, instance vars, and perhaps maybe even Procs that are within your subclass?

                      That said... you should be able to have proc refs, if they are instance @refs because they are likely to have refs inside them that ref instance objects.

                      If the class is a singleton (most WebDialogs are,) just limit the number of instances to 1, in the wrapping namespace, the same way you would for a tool instance.
                      0
                        I'm not here much anymore. But a PM will fire email notifications.
                        User avatar
                        Dan Rathbun 
                        PluginStore Author
                        PluginStore Author
                         

                        SketchUcation One-Liner Adverts

                        by Ad Machine » 5 minutes ago



                        Ad Machine 
                        Robot
                         



                         

                        Return to Developers' Forum

                        Who is online

                        Users browsing this forum: No registered users and 16 guests

                        Visit our sponsors: