What happens when i declare new vector(x,y,z) in update method.

when i declare some value for example velocity = new Vector(x,y,z)… does this vew vector allocated new space each time the update loop runs? Because if each time when i do velocity = new Vector3 memory i allocated for that value a huge portion of resources will be occupied by garbage.
Hopefully i am clear.
Can someone please explain me what happens behind the scene.

Nothing will happens since Vector3 is a struct and therefore a value type. The new keyword has a different meaning compated to other languages. It’s required whenever you want to call a constructor of a class. Though that does not automatically mean that memory is allocated. All reference types live in the heap memory. However value types are generated on the stack. They can be stored on the heap, either as part of a reference type or as a boxed value, though valuetypes in general do not live on the heap. So using “new” with struct types is just a way of calling an initializer method.

In C# all the memory that is allocated for reference types when they are generated is zero filled. Therefore all values types get their default values (which is always just 0). Value types that are “allocated” on the stack inside a method do not get their memory location cleared. That’s why the compiler won’t allow you to use an uninitialized value type inside a method. When you enter a method the compiler generates a “stack frame” for the method. This typically includes space for the parameters of the method as well as space for all local variables. Though no memory is actually “allocated”. A stack frame is simply generated by moving the stackpointer around. This memory also doesn’t need to be garbage collected as it’s “freed” when the stack pointer is essentially reset when you leave the method.

Using new Vector3() does not allocate any memory and does not cause any garbage memory generation.