I'm writing a simple Scala & Squeryl application. For test purposes, each time I run 'test' in sbt, an in-memory H2 db is created and populated with test data. After each run I can see that memory usage of java.exe (inside which sbt is running) in the Task Manager increases until after 4 or 5 runs it crashes with OutOfMemoryError. Am I missing something that explicitly releases memory used by H2 or Squeryl? By now, I use only Session.create
and then Persistence.create
. Here is an excerpt from my code:
object Persistence extends Schema {
val documents = table[IncomeEntity]
val positions = table[Position]
val documentToPositions = oneToManyRelation(documents, positions).via(_.id === _.id_income)
}
class PersistenceTests extends FunSuite with BeforeAndAfterAll {
override protected def beforeAll() {
Class.forName("org.h2.Driver")
SessionFactory.concreteFactory = Some(
() => Session.create(DriverManager.getConnection("jdbc:h2:mem:test"), new H2Adapter)
)
}
test("DDL") {
transaction {
Persistence.create
assert(Persistence.documents.size == 0)
assert(Persistence.positions.size == 0)
}
}
test("Insert") {
transaction {
Persistence.create
(1 to 100) map { _ => IncomeMapper.save(new Income) }
assert(Persistence.documents.size == 100)
}
}
}
The messages I get are simply the following:
[info] PersistenceTests:
sbt appears to be exiting abnormally.
The log file for this session is at C:\Users\Oleg\AppData\Local\Temp\sbt7320472784033855835.log
java.lang.OutOfMemoryError: PermGen space
Error during sbt execution: java.lang.OutOfMemoryError: PermGen space
Add the following flags to your SBT start-up script:
-XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m
.
That should take care of the problem.
UPDATE: If you're still crashing JVM take a look at SBT-revolver + JRebel: https://github.com/spray/sbt-revolver. It will start your app in a forked JVM so your SBT should never crash.