[SOLVED] Performance issues

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

[SOLVED] Performance issues

Post by xNick1 »

Hi guys,

As I'm playing my game, the RAM usage goes higher and higher.
That's ok, because I spawn objects and stuff and I have to update more stuff at once.

However when I change gamestate, I clear the tables which contained the objects and everything. Still the ram usage doesn't decrease.
It seems like a garbage collector problem?
How do I force the game to destroy stuff?

On Pc it isn't a problem. It has a 600mb RAM peak, it doesn't go higher, but on mobile it could be a problem.

When the day ends, I reset stuff and change gamestate.
Shame on me for not using a gamestate lib, but I'm so ahead in the development that I'd prefer to solve my problem manually.
I guess I'm doing something different from a gamestate library at this point

Code: Select all

if day == false then
        backgroundMusicSummer:stop()
        nightSummer.alpha = 0
        playerSummer.x = wWidth / 2
        camera.x = 0
        trees = {}
        stones = {}
        statues = {}
        day = true
        gamestate = "level3"
    else
        if introFadeInSummer.ended == false then  
            introFadeInSummer:update(dt)
        end
        backgroundMusicSummer:update(dt)
        spawnTreeSummer(dt)
        collisionsTreeSummer()
     
Last edited by xNick1 on Wed Jun 21, 2017 9:59 am, edited 1 time in total.
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: Performance issues

Post by erasio »

Does it increase afterwards again at any point?

Because otherwise I'd say it's most likely due to optimization. You don't usually free up ram again after you know you need that total amount of ram. Each allocation / deallocation costs time and managing the ram internally is a lot faster. So it keeping it is desirable in most cases.

Have you taken a look at the ram used? Is it actually still used? Or just allocated? You can create a memory leak with löve though. So it might be possible and you'd have to search what elements aren't getting freed up.

Otherwise. Try to get the max usage down if you aim for mobile (which might be a good idea regardless. 600MB are a lot! For example have you made sure to load every image only once and storing it in a resource manager instead of calling "newImage" every time you need one? That trick alone shaved off 200MB in my project ;)
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Performance issues

Post by xNick1 »

Oh man, I'm calling the new image function every time I create an object instead of using the old one like you said.
Thanks you so much man, I didn't think about that.
My bad =)
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: Performance issues

Post by erasio »

Fortunately it has a very simple solution!

In my cases at least I am fine with the default behavior being through my resource manager.

So I store the original "newImage" function in a different variable and provide my own function to replace newImage. That way I can explicitly call the original function while by default everyone who works on the project (regardless whether they're aware of this or not) will use the correct, cheap version :)

Code: Select all

love.graphics.originalNewImage = love.graphics.newImage
love.graphics.newImage = function(filename)
	If not images[filename] then
		images[filename] = love.graphics.originalNewImage(filename)
	end
	return images[filename]
end
Just give you an idea of what I mean. I use the filename as key which isn't all that efficient but it's really simple to implement.

This obviously should be extended and done to align well with your game so far. As I mentioned. It's just to give you an idea about how to do it.

(For example this code right now really poorly supports the newImage functions that are not simply the filepath but for example compressed image data. Which should be fixed by handling the specific cases of input separately)
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Performance issues

Post by xNick1 »

I guess something like that should do it?
imgHouse is now the same across all the instances, I only load it once.
I was loading it inside the :new function before xD

Code: Select all

local imgPath = "assets/images/spring/"
local imgHouse = lg.newImage(imgPath .. "house/house.png")

function HouseSpring:new(x)
    self.x = x
    self.img = imgHouse
    self.y = 0
end
The RAM usage is definitely lower :D
Thanks again :D
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 126 guests