Created
April 13, 2019 04:21
-
-
Save huynhjl/00a9ee6958f1b0143b701eb7b2563005 to your computer and use it in GitHub Desktop.
tensorflow-scala-0.4.1 resource leak
This file contains 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
import org.platanios.tensorflow.api._ | |
object MemLeak { | |
val rt = Runtime.getRuntime | |
def mb(bytes: Long): String = f"${bytes.toFloat / 1024 / 1024}%.1fMB" | |
def printMemory(): Unit = { | |
rt.gc() | |
val total = rt.totalMemory | |
val free = rt.freeMemory | |
println(s"total=${mb(total)} free=${mb(free)} used=${mb(total - free)}") | |
} | |
def test(): Unit = { | |
val graph = Graph() | |
val session: Session = Session(graph) | |
try { | |
for (_ <- 0 until 1024 * 10) tf.createWith(graph)(tf.placeholder[Float](Shape(-1, 2, 2))) | |
} finally { | |
session.close() | |
graph.close() | |
} | |
} | |
def testLoop(count: Int): Unit = { | |
for (i <- 0 until count) { | |
println(s"testLoop $i") | |
test() | |
printMemory() | |
} | |
} | |
def main(args: Array[String]): Unit = { | |
val count = if (args.length > 0) args(0).toInt else 5 | |
testLoop(count) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running this program prints: