Memory leak when drawing (2D game)

Hello I have a question regarding the way i deal with drawing a scene.

TL;DR What technique do you use to draw Tiles in your 2D game?

I first populate my tile map (essentially a 2D array of Images) once.

Once the 2D array is populated, i populate a buffered image, i set it to be the size of the 2D array (number of elements x size of image, etc)

  • Once the dimensions of said buffered image are done, i draw each image of the 2D array in the correct x and y location of the buffered image (start at 0, 0, then 0, 32, etc…)

So now i have this big buffered image, which is the size of the entire map (about 200x32px wide and 200x32px tall for the most part)

however, the issue comes when i draw the buffered image:

my draw function’s signature is as follows:
public void draw( final Graphics2D g,
final int xOffset,
final int yOffset,
final int width,
final int height) { … }

It basically takes a point and how wide and tall to draw (monitor size for example), once i have this information, I call the bufferedimage’s subImage function, which takes in
an x and y location as well as the width and height, this returns the desired part of the big buffered image, that is what I draw.

However, I believe this causes memory leaks, I ran the profiler and generations usually go up at random intervals, all the way up to 8, probably more but I got tired of running the game.

My question is, what would be a better way of drawing a particular scene in the game?

Yeah, when I draw a tiled map, I start with a blank screen and only draw the tiles that are visible. All my tiles are given an x-axis and y-axis location based on the location on the map. When the map moves, I also move those values around. All values that fall within a certain range on the screen is drawn, everything else is not. Works pretty quick for those types of games.

right but do you traverse the 2D array every single draw call?

assuming 32x32 pixels fit on the screen that’s 1024 iterations over the 2D array, every single draw call, I remember I tried that but it was much too slow, but I could try again see if it works.

It is technically the “draw()” call that is causing the slow down. I prevent the speed drop by skipping the draw call on the iterations where tiles won’t be drawn. You can only have so many visible items on the screen at once.

It’s one iteration of length 1024. Which is not a whole lot, actually. I tend to store my map data in a 1d array and address it as map[y * width + x], but an array of arrays also works just fine.

Memory usage does normally increase if you use any locally defined variables in the main loop. Anything that is created on the heap requests more memory. Some library calls also use heap space and request memory. When too much heap has been used, the garbage collection cuts in and profiled memory usage drops again. Unfortunately that creates a pause in the game.

The best solution is to pre-allocate all the structures you need outside the main game loop.

True memory leaks are fairly rare these days. Problems usually occur when you shell out to execute a dos command (on windows) and don’t read the output and error streams to EOF before closing the process. Another common cause is using the JNI to call native code, which allocates structures on the process’s heap, which one then forgets to keep track of.

One other possibilitiy of a memory leak is forgetting to kill threads, or not making them Daemon…
Happened to me once with a very heavy-weight class (name was “World”, around 150-210 MB).

Also, don’t use the sub-image method of BufferedImage which is inefficient and might be creating a load of extra garbage. Just use the 10 argument form of drawImage(). See http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawImage

Note it’s corners and not width / height!