Memory behavior of String#dup

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Memory behavior of String#dup


    Hi all,

    does String#dup also copy the byte sequence of the string or does it only
    copy a reference and does a copy on write?

    robert

    Robert Klemme Guest

  2. Similar Questions and Discussions

    1. #39751 [NEW]: putenv causes string copy of freed memory region, causing crash
      From: KevinJohnHoffman at gmail dot com Operating system: Win32 PHP version: 5.2.0 PHP Bug Type: Reproducible crash Bug...
    2. #39126 [NEW]: String->float->String conversion behavior
      From: bobson at rpg dot pl Operating system: Linux PHP version: 5CVS-2006-10-11 (snap) PHP Bug Type: Unknown/Other Function...
    3. WebService Behavior memory problem
      Hi, I came across several old posts mentioning a memory leak issue in WebService Behavior. Is this still a problem (even if using IE 6)? ...
    4. Memory consumption of Ruby/mod_ruby combo on Apache [memory leak]
      > I don't think so - I think all the modules are loaded when Apache is It didn't have anything to do with IfModules or even Apache. I had a...
    5. which is better for memory; a behavior script or animate through the score
      (1) A few lists aren't going to use a lot of memory, unless you are doing something very silly with them. (2) Consider the fact that a list with...
  3. #2

    Default Re: Memory behavior of String#dup

    Hi,

    In message "Memory behavior of String#dup"
    on 03/06/30, "Robert Klemme" <bob.news@gmx.net> writes:

    |does String#dup also copy the byte sequence of the string or does it only
    |copy a reference and does a copy on write?

    When memory is already shared between strings, it does copy-on-write,
    otherwise it copies. From my observation, many of duped strings are
    modified right after the dup, so that I felt it is wise to avoid
    making new internal copy-on-write entries for duping.

    matz.

    Yukihiro Matsumoto Guest

  4. #3

    Default Re: Memory behavior of String#dup


    "Yukihiro Matsumoto" <matz@ruby-lang.org> schrieb im Newsbeitrag
    news:1056962193.833961.26048.nullmailer@picachu.ne tlab.jp...
    > In message "Memory behavior of String#dup"
    > on 03/06/30, "Robert Klemme" <bob.news@gmx.net> writes:
    >
    > |does String#dup also copy the byte sequence of the string or does it
    only
    > |copy a reference and does a copy on write?
    >
    > When memory is already shared between strings, it does copy-on-write,
    > otherwise it copies. From my observation, many of duped strings are
    > modified right after the dup, so that I felt it is wise to avoid
    > making new internal copy-on-write entries for duping.
    s1 = "foo"
    s2 = s2.dup

    So, if I understand you correctly s1 and s2 don't share the same byte
    sequence since s1 is the only string referring tho the sequence "foo" when
    the dup occurs (i.e. the sequence is not shared). Is that correct?

    The question why I'm asking is, that for hashes where an entry shares the
    key (either directly because it is the same string in h[s1]=s1 or
    indirectly because the value is an instance that refers the key
    indirectly) there would be enourmous memory consumption if all those
    dup'ed hash key strings did also contain a copy of the byte sequence. The
    problem I have with this duping is that I can't prevent it. So there's at
    least the overhead of a new created String instance, because apparently (v
    1.7.3) the Hash doesn't honor the freeze state of the string.

    If that change has not been incorporated I suggest doing the dup only if a
    string is not frozen. Otherwise the user has no chance to avoid the dup
    for strings.

    Regards

    robert


    h = Hash.new

    s1 = "key 1"
    s2 = "key 2"
    s2.freeze

    h[s1]=s1
    h[s2]=s2

    h.each do |k,v|
    puts "#{k}=>#{v}"
    puts "#{k.id}=>#{v.id}"
    case k
    when s1
    p [k.equal?( s1 ), v.equal?( s1 )]
    when s2
    p [k.equal?( s2 ), v.equal?( s2 )]
    end
    end

    yields

    key 1=>key 1
    22381332=>22394808
    [false, true]
    key 2=>key 2
    22376868=>22390356
    [false, true]

    Robert Klemme Guest

  5. #4

    Default Re: Memory behavior of String#dup

    Hi,

    In message "Re: Memory behavior of String#dup"
    on 03/06/30, "Robert Klemme" <bob.news@gmx.net> writes:

    |Though I still worry about the overhead of one more ruby instance (there
    |must be some bookkeeping done etc.). Is this neglectible?

    I guess so. It's only 20 bytes per object on 32 bit CPU.

    matz.

    Yukihiro Matsumoto Guest

  6. #5

    Default Re: Memory behavior of String#dup

    Hi,

    In message "Re: Memory behavior of String#dup"
    on 03/07/02, "Robert Klemme" <bob.news@gmx.net> writes:

    |Hm, that amounts to 2 million bytes for 100000 instances - which is not to
    |much IMHO. Plus, there will be some overheads for object lookups I guess.
    |
    |I'd like to propose the change to not dup frozen strings as Hash keys.
    |Should I enter an RCR? Do we discuss this here?

    Early optimization is the source of all evil. ;-)

    Putting joke aside, frozen key string is very useful for finding
    bugs. So I think optimization should be done differently.

    matz.

    Yukihiro Matsumoto Guest

  7. #6

    Default Re: Memory behavior of String#dup


    "Yukihiro Matsumoto" <matz@ruby-lang.org> schrieb im Newsbeitrag
    news:1057156227.243350.7287.nullmailer@picachu.net lab.jp...
    > Hi,
    >
    > In message "Re: Memory behavior of String#dup"
    > on 03/07/02, Yukihiro Matsumoto <matz@ruby-lang.org> writes:
    >
    > |Putting joke aside, frozen key string is very useful for finding
    > |bugs. So I think optimization should be done differently.
    You lost me here. Maybe I wasn't clear enough and we have a
    misunderstanding. I meant - quite informally:

    class Hash
    def []=(key, val)
    if key.kind_of? String && !key.frozen?
    key = key.dup
    key.freeze
    end

    # now insert key and value
    end
    end
    > Your suggestion inspired me a new dup-freeze optimization. It'll be
    > available soon on the CVS. Thank you.
    You're welcome! Do you mean a specialized dup method that returns self if
    frozen like

    class Object
    def dupFreeze
    frozen? ? self : dup
    end
    end

    Kind regards

    robert

    Robert Klemme Guest

  8. #7

    Default Re: Memory behavior of String#dup

    Hi,

    In message "Re: Memory behavior of String#dup"
    on 03/07/03, "Robert Klemme" <bob.news@gmx.net> writes:

    |> Your suggestion inspired me a new dup-freeze optimization. It'll be
    |> available soon on the CVS. Thank you.
    |
    |You're welcome! Do you mean a specialized dup method that returns self if
    |frozen like

    <snip>

    Yes. Also this specialized dup returns hidden shared string without
    making copy if it is available.

    matz.

    Yukihiro Matsumoto Guest

  9. #8

    Default Re: Memory behavior of String#dup


    "Yukihiro Matsumoto" <matz@ruby-lang.org> schrieb im Newsbeitrag
    news:1057166023.426462.7561.nullmailer@picachu.net lab.jp...
    > Hi,
    >
    > In message "Re: Memory behavior of String#dup"
    > on 03/07/03, "Robert Klemme" <bob.news@gmx.net> writes:
    >
    > |> Your suggestion inspired me a new dup-freeze optimization. It'll be
    > |> available soon on the CVS. Thank you.
    > |
    > |You're welcome! Do you mean a specialized dup method that returns self
    if
    > |frozen like
    >
    > <snip>
    >
    > Yes. Also this specialized dup returns hidden shared string without
    > making copy if it is available.
    Sounds great! Thanks a lot!

    robert

    Robert Klemme Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139