Physics.RaycastNonAlloc allocates 40B memory. Any way around it?

According to the profiler Physics.INTERNAL CALL allocates 40B memory and I want to know if it’s possible to do something about this.

Can you show the code where you’re calling it?

1 Like

csharp** **int hits; raycastHits = new RaycastHit[10]; Ray ray = new Ray(); hits = Physics.RaycastNonAlloc(ray, raycastHits, magnitude, layerMask);** **

Which version are you on? Because I just ran that exact code, and I’m getting no allocation at all (aside from the array of course).

2657777--187402--unity_noalloc.png

1 Like

[quote=“lordofduct, post:4, topic: 629088, username:lordofduct”]
Which version are you on? Because I just ran that exact code, and I’m getting no allocation at all (aside from the array of course).

[/quote]
I’m on 5.3.4f1. Thanks for the fast responses.

e: If the ray doesn’t hit anything I don’t get any garbage but if it does hit I get those 40 bytes.
e2: I only get the garbage the first time the ray hits the collider, not the consecutive frames.

So I’m on 5.3.4f1, and I still don’t get any garbage from it.

This is the exact first time it is fired (it’s on a mouse click):

The code I’m running:

public class ZTestScript02 : MonoBehaviour
{

    private RaycastHit[] raycastHits = new RaycastHit[10];

    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            this.DoCast();
        }
    }

    private void DoCast()
    {
        Ray ray = new Ray(this.transform.position, Vector3.right);
        var hits = Physics.RaycastNonAlloc(ray, raycastHits, 10f, -1);
    }

}

And I confirm that a result is returned (debug), hits is 1, and the first element of the array has the RaycastHit.

I have it set up with this script on a gameobject at (0,0,0) and a cube sitting 5 units to the right.

Could you recreate this in a small project and maybe upload that for me to see exactly how you do it?

And if it is a thing, probably going to need to submit said project to unity as a bug.

2 Likes

I created a new project and this is the package file:

2658645–187478–RaycastNonallocTest.unitypackage (5.09 KB)

I’m getting 0 alloc from that example package.

1 Like

Any idea why I get garbage? If I create a build of my project will it remove the garbage collection?

I’ve got no clue. Unless you’re using a different runtime than we are, we should be getting the same alloc from the same code.

I sincerely doubt that this is a 2.0/2.0 subset issue, but to make sure, have you set your player to subset?

1 Like

I’m not sure how to check this. Will try to google it.

e: I’m using 2.0.50727.1433.

Oh, I was thinking about the Api Compatability Level. You set it in Edit->Project Settings->Player.

It needs to be reduced to the “.NET 2.0 subset” for certain platforms (I fail to remember which). That’s probably not the case here - the default is 2.0, and if you don’t know about it, I doubt you have changed it.

Well, then I’m fresh out of ideas.

Worth building and profiling. Most of the time profiling in the build is more relevant then profiling in the editor.

I checked and I was using “.NET 2.0 subset” but unfortunately it didn’t make any difference when I changed to “.NET 2.0”.

Gonna see if I can find out how to do this and get back with results. Thanks.

e: Unfortunately it didn’t help and I get even bigger garbage in the standalone build (64B).
e2: Changing to use an x64 architecture results in twice the amount of allocated memory, dunno if this is of any help. Since I had the subset api compatibility level selected for some reason (don’t remember ever touching that) maybe there is some setting that yields this garbage?

Can someone please test this project with the settings and everything?

https://www.dropbox.com/s/0vm67njz5unn8w2/RaycastNonalloc.rar?dl=0

e: I’d also appreciate if someone could upload a zipped project so I could test on my own computer.

I gave this a quick test on 5.3.4f1 and didn’t manage to get any allocations either :frowning:

1 Like

Wow, I really appreciate all the help you offer me guys. Did you try the complete project I linked just above your post?

Yeah, I used the dropbox project. I also modified it quickly to run 10,000 raycasts which all hit the cube and still had no allocations so I’m not sure what’s different on your build.

2 Likes

Thanks a lot for testing it. I’m really surprised you didn’t get any memory allocation. What could possibly be wrong then? Some Unity settings? Am I forced to reinstall Unity?

e: I reinstalled Unity and that didn’t help at all. Any ideas?