Created
April 20, 2020 04:22
-
-
Save esafirm/11ddff48914cf8f4e2841a1a4c930963 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MemoryProfilerRule : TestRule { | |
override fun apply(base: Statement?, description: Description?): Statement { | |
return MemoryProfilerWatcher().apply(base, description) | |
} | |
private inner class MemoryProfilerWatcher : TestWatcher() { | |
private val runtime = Runtime.getRuntime() | |
private var before: Long = 0 | |
private var after: Long = 0 | |
override fun starting(description: Description?) { | |
before = getCurrentlyAllocatedMemory() | |
} | |
override fun finished(description: Description?) { | |
after = getCurrentlyAllocatedMemory() | |
println(""" | |
Before: $before | |
After: $after | |
""".trimIndent()) | |
} | |
fun getCurrentlyAllocatedMemory(): Long { | |
return (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment