vrld / HC

General purpose collision detection library for the use with LÖVE.
http://hc.readthedocs.org/
404 stars 48 forks source link

severe memory leak when using "moveTo(x, y)" even if it's uncalled on a for loop #61

Closed EduardoGodoy closed 2 years ago

EduardoGodoy commented 6 years ago

this might be an issue on my side but i have this function "rect[j][i]:moveTo(x-tile_x+32,y-tile_y+48)" on a FOR loop that draws an isometric map (the only thing that it does it that if i move the map it will also move the collision boxes to follow the map) and even if i don't call this function and leave it inside a conditional like: it will only execute when i do a mouseclick, it causes a memory leak that takes 3gb of my ram, if i don't have this function the program takes less than 50mb of ram.

vrld commented 5 years ago

Just to check if I understand correctly: There is a memory leak even if the function is not called?

EduardoGodoy commented 5 years ago

yep, just the fact of me adding it into any part of the code, even a completely separate function that is not called it will cause a memory leak over the course of a few hours, after like 3 hours of running it will get at 5fps, if i comment this function out my program will go completely stable for such period of time. i decided to just make collision detection by myself after all.

vrld commented 5 years ago

That is very odd. Just adding the function to the code (but not calling it) should not do anything, especially not waste 3 gigs of RAM. I know I'm very late to respond, but do you still have some code that shows this memory leak?

EduardoGodoy commented 5 years ago

unfortunately i don't have it anymore, it was probably because i had 4096 different bounding boxes (i had one for each tile on a 64x64 grid)

EduardoGodoy commented 5 years ago

it wasn't expected though, when i made my own collision detection it didn't get that memory leak, i was using this library because i wanted the collision box to fit perfectly into the isometric shape by using the polygonal shape, as you can see it's taking a lot less ram even with 4096 boxes, i don't have the older version so i can't take a screenshot to compare less

vrld commented 5 years ago

Yeah, 4096 boxes shouldn't take up that much space. The box is defined by 4 points, so there are 8 numbers per shape. Lua numbern are double precision floats, so just the coordinates should be 4096 * 2 * 4 * 8 Byte = 256 MB. There is some overhead for the tables, spatial hash, etc., but it shouldn't be so much to fill 3GB.

I will try to reproduce this. If anyone has the same issue: please share!

vrld commented 5 years ago

I've been running this code for about an hour:

HC = require '.'

local shapes = {}
function love.load()
  for i = 1,64 do
    for k = 1,64 do
      shapes[#shapes+1] = HC.rectangle(i*80-200, k*80-200, 80,80)
    end
  end
end

local t = 0
function love.update(dt)
  t = t + dt
  local dx,dy = math.sin(t*math.pi*2 / 11), math.cos(t*math.pi*2 / 17)
  for _,s in ipairs(shapes) do
    s:move(dx,dy)
  end
end

function love.draw()
  for _,s in ipairs(shapes) do
    s:draw()
  end
end

But the memory consumption stays around 750MB (see screenshot). I'll leave this open fo a while in case someone can reproduce this bug.

vmem

EduardoGodoy commented 5 years ago

note: i didn't use rectangles, i used polygons so they can fit into the isometric shape (they were 4 sided but i had to use polygons so they could get into a diamond shape)

vrld commented 5 years ago

Rectangles are also polygons.