How to free java memory / Snappy


I am trying to create a new product from Sentinel 3 images, and delving deep into the SNAP API, trying to learn how to implement SNAP functions in Python. The learning curve is steep!

At the moment, I am opening a S3 image, reading a band, doing operations on it, then trying to save it using the readPixels and setPixels operators. As I am learning, I run the code a lot of times, tweaking different lines, but quickly run into memory problems. I can run my code fine at first, but after a few runs, get the following:

RuntimeError: java.lang.OutOfMemoryError: Java heap space

I use the dispose() command to close my opened image at the end. Is there a way to explicitely flush the memory used by Java after each run? Or am I missing something fundamental?



Edit: I have increased the memory in snappy.ini to 8G

1 Like

You say you start several times. So freeing the JVM memory will probably not help. Because with every run a new JVM is started, with new memory.
But you can try to invoke the garbage collection of the JVM. This would look like the following:

import jpy
System = jpy.get_type(‘java.lang.System’)

When using readPixels you should consider doing your computation linewise or in tiles and not the whole band at once.

Increasing the JAVA memory in the settings and processing by line helped resolve the problem. I was reading in and writing per band which didn’t help.

Thanks for the input!

The flag -Xmx < memory > specifies the maximum memory allocation pool for a Java Virtual Machine (JVM), while -Xms< memory > specifies the initial memory allocation pool. The memory flag can also be specified in multiple sizes, such as kilobytes, megabytes, and so on.


Starting a JVM like below will start it with 256MB of memory, and will allow the process to use up to 2048MB of memory:

java -Xmx2048m -Xms256m