Memory leak when recreating a Squeryl in-memory database

I am writing a simple Scala and Squeryl application. For testing purposes, every time I run 'test' in sbt, an H2 db database is created in memory and populated with test data. After each run, I see that the memory usage of java.exe (within which sbt works) in the task manager increases until after 4 or 5 starts it finishes with OutOfMemoryError. Am I missing something that explicitly frees up the memory used by H2 or Squeryl? Currently, 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 receive are as follows:

 [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 
+4
source share
2 answers

Add the following script flags to your SBT run: -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m . This should take care of the problem.

UPDATE: If you're still crashing down, the JVM take a look at SBT-revolver + JRebel: https://github.com/spray/sbt-revolver . It will launch your application in a forked JVM so that your SBT never crashes.

+3
source

Without any additional information (such as the circuit class that you are using, the OOME stack worm, etc.) this is hard to guess. One possibility is that you generate an instance of the Schema class on each test run, which is cached in a way that in my experience is very inefficient / inefficient. Make sure you have only one instance of Schema (usually just make it an object) and / or add more information to your question ...

+1
source

Source: https://habr.com/ru/post/1399637/


All Articles