Junk Byte Code for Kicks


Flash String weirdness

A long time ago...

I had this bug report in Console that FPS and memory graph is leaking memory.
Issue: http://code.google.com/p/flash-console/issues/detail?id=89
I was not able to find the exact cause of this other than the fact that if I don't update texts in graph, it doesn't show any memory problems.
I suspected back then that Strings were strange but was not able to pin point what's strange about it.

Fast forward to today

I recently learned that flash keeps 'master string' of strings when they get passed around.
See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/sampler/package.html#getMasterString()

For example, if you call String.substr(), the returned string will often actually be implemented as just a pointer into the original string, for the sake of efficiency. In normal usage, this is an implementation detail which is not visible to the user; however, it can be confusing when using a profiler to analyze your program's memory consumption, because the string may be shown as taking less memory than would be needed for the string's value. In addition, a string might be retained in memory solely because it is the master for other strings. getMasterString() allows profilers to show the user an accurate graph of string dependencies.

It sounds good, it is obviously saving memory by not copying the strings and reusing via referencing to original string where possible.

  • What if you only need to keep a tiny part of the string (say downloaded from an external file), will it know to discard the big string?
  • What if you chain a lot of strings together, will it keep references to all its parts?
  • Could it cause memory leaks?