logo       

Re: Any way to cut down on memory footprint?: msg#00428

lang.scala

Subject: Re: Any way to cut down on memory footprint?



Warren Henning wrote:
>
> I did some tests of what kind of memory footprint some sample
> operations have. I did a test where I generate 10,000,000 random
> doubles (generated by calling nextGaussian() in java.util.Random),
> storing them in an ArrayBuffer, and the memory usage was about 250 MB.
>

A rough explanation of the memory usage, using Scala terminology and a
hypothetical 32 bit JVM:-

Suppose each AnyRef takes up 8 bytes:-
4 bytes for a reference to the Class, to support virtual methods, classOf
and .isInstanceOf
4 bytes to support garbage collection and wait/notify locking

An unboxed Float would take up 4 bytes, a boxed Float would take 12 bytes.

An unboxed Double would take up 8 bytes, a boxed Double would take 16 bytes.

A normal immutable-size array of 10,000,000 unboxed Doubles takes up
80,000,000 bytes.


ArrayBuffer best case:-

An ArrayBuffer[Double] that was immediately set to 10,000,000 boxed Doubles,
using insertAll, would require 160,000,000 bytes for the boxed doubles, plus
40,000,000 bytes for the ArrayBuffer's array holding references to the boxed
Doubles, for a total of 200,000,000 bytes.

(Using ArrayBuffer[Float] instead of ArrayBuffer[Double] would save
40,000,000 bytes.)


ArrayBuffer worst case:-

Suppose 1 boxed Double was then appended to the ArrayBuffer. A new array
with double the size, i.e. 80,000,000 bytes, would be allocated by
scala.collection.mutable.ResizableArray.ensureSize
and the references in the old array would be copied across, for a temporary
memory usage of 280,000,000 bytes.


250 MB is between the best case and worst case.




> Is there any way to give the JVM a bigger heap size
> to work with when running Scala code?
>

I don't see the following listed as options to the "scala" executable, but
note that the "java" executable can be used instead.

java -help
...
-X print help on non-standard options
...

java -X
...
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
...

Remember to add the Scala runtime JAR to the classpath when executing
compiled Scala using "java" instead of "scala":-

java -Xmx500M -cp .;scala-library.jar TestMemoryUsage


--
View this message in context:
http://www.nabble.com/Any-way-to-cut-down-on-memory-footprint--tf3128881.html#a8671281
Sent from the Scala mailing list archive at Nabble.com.




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise