So, I did a small experiment and concluded that:
I can create 3543 scopes with Context.initStandardObject() in a 64MB
heap before running out of memory. This translates to circa 18.5KB of
RAM per one.
They are created in about 5 seconds, yielding 1.4 msec for creation of
one. (Java 6, Mac OS X 10.6.1 on an Intel Core 2 Duo at 2.4GHz)
When serialized, one such scope takes up 11076 bytes. Also, if you're
serializing your scopes (i.e. as part of a continuations-based
framework), you'd incur some of this anyway even if you used shared
scopes for standard objects, as part of the serialization are,
obviously, Java class descriptors. For comparison, serializing two
initStandardObject() scopes takes up only 18917 bytes. This means the
scope data itself weighs at 7841 bytes, and 3235 is one-time overhead
of class descriptors you'd incur anyway. So, your serialization
increase is 7841 bytes per saved script state. (Again, roughly - this
assumes your serialized scopes would have one instance of every native
JS class in them).
I'm not making any broad conclusions here, just wanted to share the
results of the measurements. For my use case that triggered the
measurements, this tells me I will choose not to use shared top-level
scopes, as the memory requirements and the creation performance
penalty (including the likely higher GC pressure) are acceptable. Your
mileage of course might vary.
(I'm also publishing this for discussion on Google Wave as my first
experiment in actually using it for something; search for "with:public
tag:mozilla rhino" and chime in there if you wish... )
Attila.
--
twitter: http://twitter.com/szegedi