Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Memory leak when drawing (2D game)  (Read 1036 times)
0 Members and 1 Guest are viewing this topic.
Offline thanatos1

Senior Newbie





« Posted 2013-01-05 08:54:53 »

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?



Offline ctomni231

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #1 - Posted 2013-01-05 09:03:06 »

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.

Offline thanatos1

Senior Newbie





« Reply #2 - Posted 2013-01-05 09:05:59 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ctomni231

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #3 - Posted 2013-01-05 09:13:29 »

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.

Offline sproingie

JGO Kernel


Medals: 202



« Reply #4 - Posted 2013-01-05 18:05:15 »

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.
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #5 - Posted 2013-01-06 06:10:07 »

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.

Time flies like a bird. Fruit flies like a banana.
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2013-01-06 12:05:51 »

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).

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline nsigma
« Reply #7 - Posted 2013-01-06 18:55:06 »

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!

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (19 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!