I am having a problem of high memory consumption caused by multiple instances of ItemStyles being created for same confiugration at multiple places in all my derived classes. Let's say I have a base class (B.pm ) which has some derived classes D1.pm, D2.pm , D3.pm etc. Now consider in D1.pm ItemStyle being created at two different places for different style as :

my $style1 = $tree-> ItemStyle ('text', -anchor => 'w', -font => 'Time +s 12',-justify => 'right'); my $style2 = $tree->ItemStyle ('text', -anchor => 'e', -font => 'Couri +er 16' -foreground => 'red', -justify => 'left');

Likewise in all the derived classes (D2,D3 etc) ItemStyle with exactly same combination of parameters and different combinations of parameters are getting created and used multiple times, hence consuming high memory . To get rid of this the idea is to create all the ItemStyles globally and store them in the base class and create one ItemStyle per parameter set. May be we can stringify the parameters and use that as a key in the global hash. Each time we need a ItemStyle in a derived class, then it will call a base class function (say getItemStyle ) with all the parameter set. The base class will sort the parameters, so that the order of mentioning the parameters ( -fg => 'red' , -justify => ' right' OR -justify => 'right', -fg => 'red') does NOT matter and NOT create the ItemStyle twice. If ItemStyle with that passed parameter combination is already present then I will return that and reuse otherwise I will create it once and return. Hope, I could explain my problem clearly. Please help me. Thanks. ghosh


In reply to ItemStyle causing high memory consumption by ghosh123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.