Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active December 19, 2015 22:29
Show Gist options
  • Save dacr/6028071 to your computer and use it in GitHub Desktop.
Save dacr/6028071 to your computer and use it in GitHub Desktop.
Inner block local variables without returning any reference are not freed !
#!/bin/sh
exec scala -deprecation -nocompdaemon "$0" "$@"
!#
def sleep(inSeconds:Int) = Thread.sleep(inSeconds*1000L)
def usedMemoryInMo_sillyimpl() = {
System.gc
sleep(2)
val rt= java.lang.Runtime.getRuntime
(rt.totalMemory - rt.freeMemory)/1024/1024
}
def printmem(msg:String) { println(s"$msg : ${usedMemoryInMo_sillyimpl()}Mb") }
def allocate(szInMb:Int=24) = Array.fill[Byte](1024*1024*szInMb)(util.Random.nextInt(256).toByte)
sleep(5)
printmem("1")
printmem("2")
println("Allocation done within an internal block :")
{
printmem("2-1")
{
printmem("2-1-1")
val innerAllocated = allocate()
printmem("2-1-2")
sleep(10)
printmem("2-1-3")
}
printmem("2-2")
}
printmem("3")
printmem("4")
println("Now with an allocation done within a function :")
def processing() {
val xx = allocate()
}
{
printmem("4-1")
{
printmem("4-1-1")
processing()
printmem("4-1-2")
sleep(10)
printmem("4-1-3")
}
printmem("4-2")
}
printmem("5")
printmem("6")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment