Skip to content

Instantly share code, notes, and snippets.

@huynhjl
Created June 12, 2011 03:43
Show Gist options
  • Save huynhjl/1021229 to your computer and use it in GitHub Desktop.
Save huynhjl/1021229 to your computer and use it in GitHub Desktop.
Memory Footprint
object HeapDumper {
import javax.management.MBeanServer
import java.lang.management.ManagementFactory
import com.sun.management.HotSpotDiagnosticMXBean
private val HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
// field to store the hotspot diagnostic MBean
private lazy val hotspotMBean: HotSpotDiagnosticMXBean = {
val server = ManagementFactory.getPlatformMBeanServer
ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, classOf[HotSpotDiagnosticMXBean])
}
/**
* @param fileName name of the heap dump file
* @param live only dump the live objects
*/
def dumpHeap(fileName: String = "heap.bin", live: Boolean = true) {
val file = new java.io.File(fileName)
if (file.exists) file.delete
hotspotMBean.dumpHeap(fileName, live)
}
}
import scala.collection.generic.SeqFactory
import scala.collection.generic.GenericTraversableTemplate
object Cell
object Test {
val size = 1000000
val time = 1000
def test[CC[X] <: Seq[X] with GenericTraversableTemplate[X,CC]](
o: SeqFactory[CC]
) {
val data = o.fill(size)(Cell)
val sz = data.size // force computation
val name = o.getClass.getName.replaceAll("""[^\p{Alnum}]+""", "")
System.gc
HeapDumper.dumpHeap("heap/" + name + ".bin")
}
def array() {
val data = Array.fill(size)(Cell)
System.gc
HeapDumper.dumpHeap("heap/array.bin")
}
def main(args:Array[String]) {
test(List)
test(Vector)
test(Stream)
test(collection.immutable.Queue)
test(collection.immutable.Stack)
test(List)
test(Vector)
test(Stream)
test(collection.immutable.Queue)
test(collection.immutable.Stack)
array()
}
}
object HeapDumper {
import javax.management.MBeanServer
import java.lang.management.ManagementFactory
import com.sun.management.HotSpotDiagnosticMXBean
private val HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
// field to store the hotspot diagnostic MBean
private lazy val hotspotMBean: HotSpotDiagnosticMXBean = {
val server = ManagementFactory.getPlatformMBeanServer
ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, classOf[HotSpotDiagnosticMXBean])
}
/**
* @param fileName name of the heap dump file
* @param live only dump the live objects
*/
def dumpHeap(fileName: String = "heap.bin", live: Boolean = true) {
val file = new java.io.File(fileName)
if (file.exists) file.delete
hotspotMBean.dumpHeap(fileName, live)
}
}
// vim: set ts=2 sw=2 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment