Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V4008. Unity Engine. Avoid using memory…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

V4008. Unity Engine. Avoid using memory allocation Physics APIs in performance-sensitive context.

Feb 04 2025

The analyzer has detected that the Physics.RaycastAll, Physics2D.OverlapSphere memory allocation methods, or similar methods are used in the frequently executed code.

Such methods create an array on each call, which can negatively affect performance. Instead, use non-allocating variants of methods to which you can pass a pre-allocated array.

Look at the following example:

public class HitScript : MonoBehaviour
{
  void Update()
  {
    AllocMethod();
  }

  void AllocMethod()
  {
    RaycastHit[] hitsTargets = Physics.RaycastAll(transform.position, 
                                                  transform.forward);

    foreach (RaycastHit hit in hitsTargets)
    {
      if (hit.collider.gameObject.name != "PlayerAdvanced")
      {
        ....
      }
    }
  }
}

Each frame, Update calls AllocMethod that uses the RaycastAll allocation method. Due to the constant creation of new arrays containing ray hits, performance will degrade.

This code can be optimized. To do this, replace the RaycastAll method with the non-allocating RaycastNonAlloc method and use it together with the pre-allocated array.

An optimal implementation option:

public class HitScript : MonoBehaviour
{
  void Update()
  {
    NonAllocMethod();
  }

  const int MAX_RAYCAST_HIT_COUNT = 10;
  RaycastHit[] _hitsTargets = new RaycastHit[MAX_RAYCAST_HIT_COUNT];

  void NonAllocMethod()
  {
    int countOfResults = Physics.RaycastNonAlloc(new Ray(transform.position,
                                                         transform.forward ),
                                                 _hitsTargets);

    for(int i = 0; i < countOfResults; i++)
    {
      if (_hitsTargets[i].collider.gameObject.name != "PlayerAdvanced")
      {
        ....
      }
    }
  }
}

The _hitsTargets auxiliary array is created in advance to use RaycastNonAlloc. The maximum number of ray hits is limited by the size of this array. After calling the RaycastNonAlloc method, the number of ray hits is written to the countOfResults variable, and the hits are written to the _hitsTargets array. Thus, each frame will use the _hitsTargets array, for which memory is allocated only once.

At the time of writing the documentation for this diagnostic rule, all methods in the Physics2D class with the "NonAlloc" prefix are marked as deprecated and can be replaced by non-allocating overloads of normal methods. For example, instead of Physics2D.BoxCastAll, use the Physics2D.BoxCast overload that takes an array to get all the hits.

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam