Do memory pools actually work in love2D

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.
User avatar
Gunroar:Cannon()
Party member
Posts: 141
Joined: Thu Dec 10, 2020 1:57 am

Do memory pools actually work in love2D

Post by Gunroar:Cannon() » Thu Dec 24, 2020 6:55 pm

A while ago after reading about memory pools ( https://gameprogrammingpatterns.com/object-pool.html ) I implemented it to my class lib and I can't see any changes. I don't really think it should increase FPS and I think the improvement is from memory? I like to think it works but does it? And how?
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
    for x, enemy in ipairs(self.allEnemies) do
        self:Cannon(enemy)
    end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

dezoitodemaio
Prole
Posts: 15
Joined: Mon Oct 26, 2020 2:02 pm

Re: Do memory pools actually work in love2D

Post by dezoitodemaio » Fri Dec 25, 2020 3:55 am

As far as i know memory pooling is a good choice if youre creating and deleting objects during the gameplay, outside the load/initialize part of your game.

Allocating/deallocating memory is a expensive operation, thats why object pooling is good. You can reuse the memory previously allocated by another object.

If you are not creating/deleting objects constantly i dont see any benefits on using memory pooling.

User avatar
ivan
Party member
Posts: 1703
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Do memory pools actually work in love2D

Post by ivan » Fri Dec 25, 2020 7:02 am

Lua is a scripted language with "managed" memory so generally speaking we don't have memory pools.
We can't explicitly allocate or free memory in Lua because the Lua interpreter does that on its own.
Having said that, you can create your objects in advance to avoid slowdown during gameplay.

User avatar
Gunroar:Cannon()
Party member
Posts: 141
Joined: Thu Dec 10, 2020 1:57 am

Re: Do memory pools actually work in love2D

Post by Gunroar:Cannon() » Fri Dec 25, 2020 10:04 am

Thnx for the replies. What I got from this is that it should stop slow loading when new instances are made.(?)Though can't see changes in mine :P(I am creating lots of the same object like bullets and smoke)..
Oh and meRRy Christmas...
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
    for x, enemy in ipairs(self.allEnemies) do
        self:Cannon(enemy)
    end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

User avatar
Gunroar:Cannon()
Party member
Posts: 141
Joined: Thu Dec 10, 2020 1:57 am

Re: Do memory pools actually work in love2D

Post by Gunroar:Cannon() » Fri Jan 08, 2021 10:31 pm

New thought plsss!! thnxxx!!
If memory pools have an effect then shouldn't collectgarbage('count') be more or less steady(like maybe 5mb fluctuations)? Is that how it works. Mine isn't too steady(100mb flucs). Pls answer, pls thnx, thnx pls, answer thnx.
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
    for x, enemy in ipairs(self.allEnemies) do
        self:Cannon(enemy)
    end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

MrFariator
Party member
Posts: 211
Joined: Wed Oct 05, 2016 11:53 am

Re: Do memory pools actually work in love2D

Post by MrFariator » Sat Jan 09, 2021 11:36 pm

Pooling can help, and I've successfully used it to reduce memory footprint in my projects, but it won't magically fix your memory issues if your objects generate a whole bunch of garbage (temporary tables, closures, etc). That garbage will pile up over time, until the garbage collector will run its sweep, which can be configured to suit your needs on a case-by-case basis (but isn't much of a solution, more like a hotfix).

We don't know what your code does unless you share it, so I recommend using something like profile.lua or jprof to diagnose where most of the memory build up is happening and when.

User avatar
Gunroar:Cannon()
Party member
Posts: 141
Joined: Thu Dec 10, 2020 1:57 am

Re: Do memory pools actually work in love2D

Post by Gunroar:Cannon() » Mon Jan 11, 2021 11:15 am

MrFariator wrote:
Sat Jan 09, 2021 11:36 pm

We don't know what your code does unless you share it, so I recommend using something like profile.lua or jprof to diagnose where most of the memory build up is happening and when.
There isn't really anything wrong with my code, really. I just wanted to know if I'm doing object pooling right and if it has an effect (but thnx for the links).

So, besides other table junk, yes or no, Should the count stay more or less the same or is it normal to still fluctuate at the levels as if you didn't use object pooling???

By the way you can find my code somewhere here: https://love2d.org/forums/viewtopic.ph ... 5&start=40
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
    for x, enemy in ipairs(self.allEnemies) do
        self:Cannon(enemy)
    end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

User avatar
pgimeno
Party member
Posts: 2483
Joined: Sun Oct 18, 2015 2:58 pm

Re: Do memory pools actually work in love2D

Post by pgimeno » Mon Jan 11, 2021 1:57 pm

I believe that what MrFariator is trying to convey is that the pool only helps with the objects that are pooled, but there are many other possible sources of objects that are not pooled, but need to be cleaned up by the garbage collector, including strings, Löve objects, functions, coroutines and other temporary tables. Without the whole program, it's impossible to tell whether some part of it is generating objects that count as collectable garbage.

But even in the part you copied, in your class.lua, in function log(), you have this line:

Code: Select all

    file:write(str.."\n")
That creates a temporary string resulting of concatenating the passed string and a newline character. This temporary string needs to be cleaned up by the garbage collector. As you call function log(), new strings will keep being generated, and memory will grow, until the garbage collector fires.

Similar temporary objects may be created elsewhere. There's been a recent discussion in this thread, related to whether it's possible to write a game that doesn't generate any garbage: https://love2d.org/forums/viewtopic.php ... 82#p237582 - apparently Xii is able to do so in his game, but it's quite uncommon and requires quite some effort.

User avatar
Gunroar:Cannon()
Party member
Posts: 141
Joined: Thu Dec 10, 2020 1:57 am

Re: Do memory pools actually work in love2D

Post by Gunroar:Cannon() » Mon Jan 11, 2021 8:16 pm

:P ... yes...or ...no(unrelated to whatever problems I may have and (theoretically) besides all those other strings and stuff.) (You have made more things clear though. t)
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
    for x, enemy in ipairs(self.allEnemies) do
        self:Cannon(enemy)
    end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

User avatar
pgimeno
Party member
Posts: 2483
Joined: Sun Oct 18, 2015 2:58 pm

Re: Do memory pools actually work in love2D

Post by pgimeno » Mon Jan 11, 2021 9:09 pm

It is normal that it fluctuates if you generate garbage. It isn't normal if you don't. We can't know if you do or you don't, but it seems you do, judging by the snippet I've posted above.

Post Reply

Who is online

Users browsing this forum: No registered users and 75 guests