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: 1088
Joined: Thu Dec 10, 2020 1:57 am

Do memory pools actually work in love2D

Post by Gunroar:Cannon() »

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?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
dezoitodemaio
Prole
Posts: 15
Joined: Mon Oct 26, 2020 2:02 pm

Re: Do memory pools actually work in love2D

Post by dezoitodemaio »

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: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Do memory pools actually work in love2D

Post by ivan »

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: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Do memory pools actually work in love2D

Post by Gunroar:Cannon() »

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...
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Do memory pools actually work in love2D

Post by Gunroar:Cannon() »

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.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Do memory pools actually work in love2D

Post by MrFariator »

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: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Do memory pools actually work in love2D

Post by Gunroar:Cannon() »

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
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Do memory pools actually work in love2D

Post by pgimeno »

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: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Do memory pools actually work in love2D

Post by Gunroar:Cannon() »

: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)
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Do memory pools actually work in love2D

Post by pgimeno »

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 164 guests