添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I've been giving my first steps with images and buffered images in Java (and Java itself) and I'm a little confused about the flush() method, and My question is quite simple: When is it safe or even mandatory to flush an image in the code?

Doing some tests it looks like Image.flush() doesn't do anything, but BufferedImage.flush() gives some random results (sometimes some memory seems to be freed), but the component I use to paint it stops painting it on it's background.

So should I ever use Image.flush() or BufferedImage.flush() or is that something that I have to let the JVM do on it's own, or maybe do it in the finalize() method of an object?

I really can't figure it out...

You never need to call flush() , unless you want to free up memory. It basically just serves as a hint to the object to say, "Hey, go ahead and remove all your backing memory buffers now, instead of waiting for garbage collection to take care of you." It can be a useful performance optimization but it's never necessary to call.

According to the Java docs, calling flush() should leave the image in a state where it can reconstruct itself as necessary, but obviously you're running into issues where that isn't the case. Basically, don't call flush() unless you're sure you no longer need the image.

EDIT: According to a comment by @NorbertM, there are situations where an image won't be seen as deletable by the garbage collector, possibly due to image pooling or other optimizations running in the background. Basically, you should always call flush() on an image as soon as you're done with it (but no sooner).

So calling it in the finalize() method of the image's owner doens't make any sense either, right? Since the owner's being finalized, the image is also being flushed (unless it's referenced elsewhere) Am I wrong? Thanks for your answer :) osr Oct 3, 2011 at 22:19 It shouldn't hurt to call it in your finalizer, and in some cases it can actually help, since depending on your particular garbage collector, if you don't call it in finalize() then the underlying image can still live on for another GC cycle since it's still owned during the mark phase of the mark-and-sweep. Just make sure that if you do call it in finalize() that this object is the only one using it. fluffy Oct 3, 2011 at 22:25 You NEED to call flush() in some circumstances, I have learned today: "Image objects created by the Toolkit and Component classes which are loaded from files, URLs or produced by an ImageProducer are unloaded and all local resources are released." At least with in my App the garbage collector was not able to free all Image resources (probably native ones) and I always ended up with: Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space after consecutive loads of quite large images with this.toolkit.createImage(byte[]). It was PNG images. NorbertM Mar 30, 2022 at 0:44 @NorbertM Oh, interesting! I've never run into that issue. I wonder if that might be due to optimizations which came to be in the 10+ years since I answered this question. :) (Also, that said, I did say "unless you want to free up memory" which is technically the situation you're describing.) fluffy Mar 30, 2022 at 17:33

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .