Searles
Searles

Reputation: 1827

Avoid OutOfMemory when creating an image

I want to create a BufferedImage in a Java-Application. The users are allowed to change the dimensions of the image and I would like to allow images "as big as possible". How can I check, whether creating the BufferedImage would cause an OutOfMemory-Exception and recover from it?

Upvotes: 0

Views: 103

Answers (3)

Johan Nordli
Johan Nordli

Reputation: 1230

Use a try catch is a simple way to do this

Example here

Good luck

Upvotes: 1

Makky
Makky

Reputation: 17461

Try to allocate as much as memory when running application.

-Xms256m -Xmx1024M

Above argument will allocate 1GB memory.

Upvotes: 0

pbespechnyi
pbespechnyi

Reputation: 2291

Use Runtime.getRuntime().freeMemory() to check the free memory of your JVM instance. And then based on this value you can add restrictions for dimensions for future image.

Upvotes: 1

Related Questions