Click here to Skip to main content
15,078,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a bufferedImage which is getting drawn to and then rendered to the screen every frame. The issue is that for some reason, when getting the graphics from it, it leaks some memory, usually about 0.1-0.2 mb per iteration.

here is some of the code I used:
//v v v v in constructor
BufferedImage bi;
bi= ImageLoader.gc.createCompatibleImage(Display.getWidth(), Display.getHeight(), Transparency.TRANSLUCENT);
//^ ^ ^ ^ in constructor

public void render() {
		bs = display.getCanvas().getBufferStrategy();
		if(bs  == null) {
		g = (Graphics2D)bs.getDrawGraphics();
		g.clearRect(0, 0, Display.getWidth(), Display.getHeight());
		Graphics g2 = bi.createGraphics();
		g2.clearRect(0, 0, bi.getWidth(), bi.getHeight());
		g.drawImage(bi, 0,0,Display.getWidth(), Display.getHeight(), null);;

the g is a graphics object and bs is a bufferstrategy object that comes from the canvas that is made with the JFrame.

What I have tried:

I have tried disposing of the bufferedimage's graphics, and tried to flush() the image itself, but nothing seems to fix the memory leakage
Updated 3-Sep-21 7:14am
Richard MacCutchan 3-Sep-21 12:35pm
And there is nothing we can do to help without some proper details of your code.
Member 14911322 3-Sep-21 12:54pm
added the important code
Richard Deeming 6-Sep-21 5:35am
Have you actually profiled the code and determined that there is a memory leak? Or are you just looking at the amount of memory allocated to the process?
Member 14911322 6-Sep-21 8:11am
I used a profiler and it showed that the bufferedimage was suspect. Also in task manager the memory usage increases slowly forever
Richard Deeming 6-Sep-21 8:18am
Task manager on Windows is definitely not the right tool to use. It shows you how much memory is currently available for use by your application, not how much memory your application is actually using.

What is the profiler actually pointing to as the problem? It's possible that there's a memory leak inside the BufferedImage class itself, in which case there's nothing you can do to fix it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900