H2 DB in Memory Clean the RAM
Asked Answered
H

1

6

I am using H2 DB in in-memory mode to process some data. My java application needs to keep the JVM on while dropping and re-creating or truncating or dropping all objects in H2 DB in each run. Therefore I need to use the option 'DB_CLOSE_DELAY=-1' in the DB URL.

To clean the memory after an iteration I tried using 'DROP ALL OBJECTS', 'DROP ALL OBJECTS DELETE FILES' and Truncated each table one by one. All of these achieved what I wanted by didn't delete the memory that was currently occupied by it. My database size varies from about 300MB to 2GB. Therefore after a single iteration my RAM is about 1GB occupied and did not find any solution as to what the cause of this or the resolution.

Somewhat similar to this Clear the in memory database after every testcase. But without the dbUnit and need to clear the RAM too.

Is it possible to do this without stopping the JVM.

Thanks.

Humbert answered 11/2, 2013 at 12:37 Comment(1)
You could execute the statement shutdown. This will close the connection and the database.Simile
S
5

You could execute the statement shutdown. This will close the connection and the database.

Simile answered 12/2, 2013 at 6:17 Comment(9)
Hello, Sorry for troubling. But the solution worked few times when I tried. But it doesn't work sometimes. My java process java.exe shows to have occupied 1.1 GB when running. After the shutdown command has been executed it was reduced to about 80 MB which was about average my program using. But I see that in the windows task manager that although process using memory has been reduced my physical memory has not been freed. It freed about 1.1 GB only after I manually end the java.exe process which was using.Humbert
I tried this few times using SHUTDOWN, SHUTDOWN COMPACT, SHUTDOWN IMMEDIATELY and DROP ALL OBJECTS with combination with previous each one. But result still the same and sometimes java.exe process also does not reduce memory as well. It keeps allocated memory of 1.1GB as it is. Have you ever come across this or am I doing something wrong. Please help.Humbert
How did you measure the memory usage? Java doesn't return unused heap memory to the operating system. But I wonder if that's really a problem for you.Simile
I only used the task manager to monitor the process which was using this database.Humbert
One thing I remembered now. I am using this in Windows 7 and I do not have Admin Rights for this computer. Is that the case?Humbert
No. It's just how Java works. See also juhonkoti.net/2010/10/19/…Simile
Specially, see "Conclusion" in the linked article: "The memory wont be released back to the OS until JVM exists"Simile
Thank you @Thomas. You are correct. The database releases the memory. However, the JVM doesn't. The excess memory is used in the next iteration of H2 DB creation so used memory does not increase unless the DB size increases in the next run.Humbert
FYI future versions of H2 will support so called "off-heap" storage using a special in-memory file system. This is using memory that is not managed by the JVM, and will allow returning the memory to the operating system when it is not used. It's a bit slower than regular in-memory databases however.Simile

© 2022 - 2024 — McMap. All rights reserved.