multitheftauto / mtasa-blue

Multi Theft Auto is a game engine that incorporates an extendable network play element into a proprietary commercial single-player game.
https://multitheftauto.com
GNU General Public License v3.0
1.42k stars 438 forks source link

Memory leak #860

Closed WADmitry closed 3 years ago

WADmitry commented 5 years ago

Describe the bug There is a memory leak. I believe the game doesn't completely free the memory used by models/textures.

For example, I use “heavy” vehicles (many polygons, hi-res textures). And then I spawn about 200 vehicles. Then I delete these vehicles and spawn again. After some times client will be crashed because of out of memory (when the client eats about 3.5 Gb).

Maybe it's related on any other resources too (skins, etc), but if I don't spawn vehicles on the server, I don't see the problem (although I didn't dig it much).

It has very small affect with default GTA models, but it is a problem with “heavy” models.

To reproduce Connect a server. Open task manager and see how many gta_sa.exe takes memory. enter reconnect (one or many times).

Another way to see the leak

srun for i=1,5000 do createVehicle(math.random(400,600), 62+math.random(-100,100), -60+math.random(-100,100), 3+math.random(50)); end

srun for i,n in pairs(getElementsByType("vehicle")) do destroyElement(n) end

repeat many times and you'll see that process takes more and more memory.

Expected behaviour No memory leaks -> no client crash

Screenshots

Version 1.5.6

Additional context I don't know if it related with the problem, but our logs contains this:

Too many objects without modelinfo structures [DUP x888]

StifflersMom commented 5 years ago

start freeroam, start runcode, sp 0 0 5,

crun testped = {}; for i=1,200 do testped[i]=createPed(0,0+i,0+i,5) end
crun i=0; setTimer( function()  for e=1,200 do setElementModel(testped[e],i) end; i=i+1 if i>300 then i=0 end end,50,10000)

See the memory consuption until

Version = 1.5.6-release-16598.0.000
Time = Tue Apr  2 19:46:12 2019
Module = C:\Program Files (x86)\Rockstar Games\GTA San Andreas\gta_sa.exe
Code = 0xC0000005
Offset = 0x00349EE4

EAX=7DE37C44  EBX=7DE37C28  ECX=7DE37C01  EDX=00000004  ESI=23A1ABA8
EDI=239230A0  EBP=238A3E34  ESP=0028F2D4  EIP=00749EE4  FLG=00010202
CS=0023   DS=002B  SS=002B  ES=002B   FS=0053  GS=002B
CrosRoad95 commented 5 years ago

I think it is releted to custom animations @saml1er added. It leaks only when peds are streamed in ⚠️ i ran debugger and code Stifflers provided what caused rapidly memory consuption ( 10-20MB per second ) and i got it: image

image

image

and magic leak in getElementsByType image

ghost commented 5 years ago

@CrosRoad95 unique_ptr is used for CAnimBlockSA throughout the code, so that shouldn't cause any memory leaks. It doesn't even allocate any memory in constructor. About CopyAnimation function handler, it only allocates an instance of CAnimBlendAssociationInterface class which is destroyed later when the animation changes, and we're using the default GTA allocator for it. When the ped streams in, GTA will add idle animation to ped, so this function is called.

CopyAnimation handler will also decompress IFP animation if needed, that will also cause a new allocation. This is how the original CopyAnimation function worked

Custom animation memory is allocated in CClientIFP.

If hundreds or thousands of peds stream in multiple times per second, then do the math, how many times CopyAnimation will be called to add idle animation? ¯_(ツ)_/¯

Dutchman101 commented 4 years ago

Perhaps this is a duplicate of issue #359? Will close it unless someone has reason to believe it's not.

Dutchman101 commented 3 years ago

Closing per no feedback on last comment.

If you still think this issue is relevant after the bunch of DFF/TXD memory leak related fixes by @saml1er (March 2020), feel free to open up a new issue