GC Allocation

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

Mrm83
Posts: 15
Joined: 17 Dec 2017, 04:02

GC Allocation

Postby Mrm83 » 13 Jun 2018, 17:48

I am syncing my player's position via UserVarableRequest when they move, i don't do it every frame, only when the min distance threshold is reached, roughly 5 times a second if they move in a straight line.
Each move requires creating a SFSUserVariabler for x,y,z. Is there a way to reuse the same object instead of creating a new one every send request? This is creating a lot of GC. (MOBILE)

Code:
userVariables.Add(new SFSUserVariable("x", (double)pos.x));
userVariables.Add(new SFSUserVariable("y", (double)pos.y));
userVariables.Add(new SFSUserVariable("z", (double)floor));

The above alone creates 20kb gc every sec when my player moves.

I thought about using SFSObject and send it as a custom requesthandler as I can reuse the object, but when I set the uservariables from server side, it doesn't notify the clients so that option won't work.
User avatar
Lapo
Site Admin
Posts: 20056
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GC Allocation

Postby Lapo » 14 Jun 2018, 07:43

Hi,
we've discussed this in an email with another client (or maybe it was you?)

We are aware of this but we're also aware that even a simple call to Debug.Log() causes similar spike in the GC graph. Memory has to be allocated when data goes out, and C# has no control over the actual RAM being used, meaning that you don't get to decide how much memory is allocated when you use the "new" keyword to instantiate objects.

We've already tested this multiple times and notice that even the simples of operations in Mono/Unity causes similar activity in the GC so I think this is pretty normal for that environment.

Code: Select all

userVariables.Add(new SFSUserVariable("x", (double)pos.x));

You can keep a reference to the old user variable and reuse it. Like this:

Code: Select all

var myUVar = new SFSUserVariable("x", (double)pos.x);

and then reuse that variable instead of using "new" every time. It should work.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Mrm83
Posts: 15
Joined: 17 Dec 2017, 04:02

Re: GC Allocation

Postby Mrm83 » 14 Jun 2018, 11:12

No it was not me.

Unfortunately, your suggestion does not work (unless I am not understanding your idea correctly).
var myUVar = new SFSUserVariable("x", (double)pos.x);
and then reuse that variable instead of using "new" every time. It should work.

How do I reuse myUVar? I can't reassign the value of "x" as it is read only. Just sending myUVar after creating it doesn't work as it doesn't seem to reference the updated value, only the value of x when myUVar was created. So in order to change the "x" value, I would still need to do "myUVar = new SFSUserVariable("x", (double)pos.x);" again and again which is pointless.

In C#, instantiating a class means gc that is why I reuse all objects (pooling) if possible to avoid having to instantiate a new object by calling "new".
We've already tested this multiple times and notice that even the simples of operations in Mono/Unity causes similar activity in the GC so I think this is pretty normal for that environment.

No, it is not normal. If we know creating a new object causes GC, we pool it and reuse it instead of creating it again and again. So not pooling or reusing is NOT normal (if the goal is to save memory).

In comparison.. my PUN production project does 0B of gc alloc every frame and gc alloc only happens once a while when new objects needs to be created.
vs
right now every 1 sec 10kb...
User avatar
Lapo
Site Admin
Posts: 20056
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GC Allocation

Postby Lapo » 14 Jun 2018, 14:46

I stand corrected, you cannot overwrite the value of an existing SFSUserVariable on the client side, so yes you would need to create a new Object.

In C#, instantiating a class means gc that is why I reuse all objects (pooling) if possible to avoid having to instantiate a new object by calling "new".

In C# nobody does that (pooling), but in Unity it is done sometimes as on optimization, where possible.
Unity itself doesn't do it that much as you can see by yourself invoking most of the existing API.

We could add an extra method on UserVar and RoomVar to overwrite the existing value, and avoid creating new instances.

No, it is not normal. If we know creating a new object causes GC,

You realize that a program in order to do something has to use resources? If C# did not run a GC you would have to spend an equal amount of CPU to clean your memory anyways. The only difference is that the GC doesn't do it exactly when you want it, and therefore you have to be careful not to overload it.

Have you measured how much CPU % is used by the GC?
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 20056
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GC Allocation

Postby Lapo » 14 Jun 2018, 15:31

For reference an empty Unity project with no assets and no game objects on the scene, besides the camera, generates ~187 allocations and 7-10Kb of GC per frame, according to its own profiler.

However these numbers have very little meaning without some context, the context being how much this impacts the actual performance of the application running. Did you investigate this aspect? Can you give us some numbers and context? (e.g. what device has been used and type of test performed)

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 20056
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GC Allocation

Postby Lapo » 14 Jun 2018, 15:58

One more thing, you can bypass entirely the UserVariables especially if you're sending tens of updates per second and instead just use an Extension request.

You can even recycle the same SFSObject if you wish, and maybe just send a single binary payload (byte[] wrapped in SFSObject) instead of structured data to avoid further steps of serialization.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 3 guests