Page 1 of 1

Memory allocations

Posted: 13 Apr 2015, 14:45
by Evil-Dog
Hi there
I was wondering something, I use a lot of extension requests, for starting to shoot the gun, when jumping, triggering items, etc lots and lots.
So there's a lot of allocations for the SFSObject like this (from your BattleFarm exemple)

Code: Select all

var paramsObj:SFSObject = new SFSObject();
paramsObj.putInt("bId", i);
paramsObj.putInt("bx", px);
paramsObj.putInt("by", py);
refDocument.smartFox.send(new ExtensionRequest("bb", paramsObj));
The same thing happens when sending user variables such as this:

Code: Select all

if(m_PlayerPawn.m_vLocation.X != m_fNetPawnLocationX)
	m_UserVars.push(new SFSUserVariable("PosX", m_PlayerPawn.m_vLocation.X, VariableType.DOUBLE));
if(m_PlayerPawn.m_vLocation.Y != m_fNetPawnLocationY)
	m_UserVars.push(new SFSUserVariable("PosY", m_PlayerPawn.m_vLocation.Y, VariableType.DOUBLE));
Isn't that worrying with the constant allocating and the garbage collector? Every list of things to do to optimize your game includes cutting down on the constant object allocations like that. I was thinking of reusing the same SFSObject and just create it once but there's no "clear" method to remove all its keys or anything. It would be tedious to remove the specific named keys (still doable). In the case of user variable maybe i could keep an instance for it and change its values I think? Would that even work? What are your thoughts on the best way to go about reducing allocations on that front?
Thank you very much

Re: Memory allocations

Posted: 14 Apr 2015, 07:40
by Lapo
In general I would not reuse SFSObjects, they're not done for reuse.

Everything you do creates new objects which need to be deallocated at some point. If you could clean up the content of an SFSObject that alone would dereference all of the data in it, thus creating garbage. :) The only way not to allocate memory is doing nothing! :mrgreen:

Objects such as SFSObject are extremely light. It's essentially just a container for your data, and it's unlikely to cause any issues unless you are going to create thousands per second.

I would not worry about it.

Re: Memory allocations

Posted: 14 Apr 2015, 13:35
by Evil-Dog
Alright cool, thanks for the info :)