Results 1 to 4 of 4

Thread: Tracking memory consumption of plugins.

  1. #1
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Tracking memory consumption of plugins.

    I've written an application which supports plugins. Now I am wondering: which is the easiest way(from the perspective of the plugin programmer) to track how much heap memory a plugin has reserved, so that it can be displayed in the main application.

    The restrictions I am currently imposing on the plugin programmer are:
    1. They must inherit an interface that looks like:
      Qt Code:
      1. class plugin
      2. {
      3. void init();
      4. void process();
      5. void reset();
      6. };
      To copy to clipboard, switch view to plain text mode 
    2. They aren't allowed to keep member or stack variables, but should allocate memory in init() and release in reset().
    3. They must include a certain header file of mine.


    Now, the perfect solution would be completely transparent and impose no further restrictions on the programmer. I was hoping I could overload the new operator for the plugin class:
    Qt Code:
    1. class plugin
    2. {
    3. ...
    4. void *operator new( size_t n);
    5. void operator delete( void *p, size_t n);
    6. void *operator new[]( size_t n );
    7. void operator delete[]( void *p, size_t n);
    8. };
    To copy to clipboard, switch view to plain text mode 
    but that isn't enough if the constructors of the objects created also call new. I could catch some cases by reimplementing the global new the same way:
    Qt Code:
    1. void *operator new( size_t n);
    2. void operator delete( void *p, size_t n);
    3. void *operator new[]( size_t n );
    4. void operator delete[]( void *p, size_t n);
    To copy to clipboard, switch view to plain text mode 
    and I guess I should do the same with malloc and free for good measure.
    But what if the plugin programmer uses classes defined in a dynamic library. Is there a way to "insert" my new operator into other libraries?

    I am not looking for a perfect solution that tracks everything. Typically an init() function will reserve a couple of images and lookup maps, etc.

    Here is an example:
    Qt Code:
    1. void init()
    2. {
    3. // Handled by my global new operator:
    4. lum = new unsigned char[256][256][256];
    5.  
    6. // OK, as long as they use my image class for which I can reimplement new
    7. img = new Image(800,600, rgb, s8bit);
    8.  
    9. // What about the stl, it relies heavily on placement new, will my new get called?
    10. // And it seems most classes has an "allocator" template argument.
    11. // The following seems to work, but is that always the case?
    12. data = new std::vector<int>;
    13. data->resize(128*128);
    14. }
    To copy to clipboard, switch view to plain text mode 

    There must be other ways as well, but it seems they all mean more restrictions(or tedious work) for the plugin programmer.

    I'm thankful for any ideas how to go about this.

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Tracking memory consumption of plugins.

    Quote Originally Posted by spud View Post
    The restrictions I am currently imposing on the plugin programmer are:
    1. They must inherit an interface that looks like:
    2. They aren't allowed to keep member or stack variables, but should allocate memory in init() and release in reset().
    3. They must include a certain header file of mine.
    1. This does not look like an interface to me... I assume it only "looks" like it then... You wouldn't get far otherwise...
    2. I'm just wondering how you expect the plugins not to use stack... All local variables make use of stack and you'll need some, even if you allocated a big bunch of data where you'd store the actual variables (which, besides bringing a lot of complexity, would be an incredible waste of memory and time IMO)
    3. Well, that's a restriction common to ANY plugin due to there very nature...
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Tracking memory consumption of plugins.

    1. Yes I have simplified things a bit
    2. I guess I could have been clearer on that point. There is no way for me and I have no intention to stop anybody from using the stack. I'm not worried about that because it won't accumulate or add up to much anyway. What I do worry about is adding images and lookup maps etc as data members. The allocation of these shouldn't happen in the constructor, but in the init() function where I can control it.
    3. Yes that is a common but necessary restriction. What I want to avoid is forcing the programmer to write code like:
    Qt Code:
    1. void init()
    2. {
    3. img = new Image(800,600, rgb, s8bit);
    4. iJustAllocated(800*600*nrOfChannels(rgb)*sizeof(s8bit));
    5. }
    6. void release()
    7. {
    8. delete img;
    9. iJustFreed(800*600*nrOfChannels(rgb)*sizeof(s8bit));
    10. }
    To copy to clipboard, switch view to plain text mode 
    So if you have any suggestion how I should do that...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Tracking memory consumption of plugins.

    You can have a stack variable with a small memory footprint that will simply be initialized in the init, like so:
    Qt Code:
    1. class .. {
    2.  
    3. private:
    4. QImage img;
    5. ...};
    6. void ...::init(){
    7. img = QImage(800,600,...);
    8. }
    To copy to clipboard, switch view to plain text mode 
    img is allocated on stack but initialized (and expanded) in init().

    BTW. In the interface those methods have to be virtual.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.