in Unity 2022.3, why do my object instantiation times spike ever so often?

So I’m currently creating a game with procedural world generation. Therefore I am loading and unloading big chunks of world data quite often. The process goes as follows:

Step 1: Upon loading a chunk, it gets a thread worker which reads its data from a file in a seperate thread.
This worker also prepares “proto-objects” that contain most of the data that is necessary to instantiate my GameObjects later. This includes things like a Color array to set a texture and a Vector2 array to set a collider.

Step 2: Once the thread is done, it puts the finished proto-objects into a ConcurrentQueue where they are dequeued in the main thread and used to initialize the GameObjects. This includes adding the script and a SpriteRenderer as components and initializing a Texture2D with the createUninitialized flag set to true.

Step 3: The Creation of the collider as well as the application of the Color array to the texture and subsequent Apply() call are handled later (which works fine and is outside of the scope of this post)

Now heres the problem:
Step 2 usually takes about 0.5-5ms.
Screenshot Profiler case 1

But every so often it spikes to taking 50-200ms.
Screenshot profiler case 2

As you can see, this happens in more or less regular intervals. I first suspected the garbage collector clears to have something to do with it because you can see it lining up with this spike here. This does not seem to be the case with the others though …
Also it seems to be texture size independent because in both cases highlighted here, the texture has a size of 160×160, which is the case most of the time…

Anyways, here’s the code that gets profiled in the highlighted “Create Objects” part:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static PhixelObject Create(Chunk chunk, Vector3Int chunkPos, List<Vector2> verticies, List<Phixel> perimeterPhixels, Phixel[,] phixelMatrix, GameObject gameObject = null)
{
//Create create
PhixelObject phixelObject = Create<PhixelObject>(gameObject);
//this just creates a new GameObject and adds the script as a component
//Set Transform
phixelObject.transform.localScale = new Vector3(Constants.PIXEL_PER_METER, Constants.PIXEL_PER_METER, 1);
phixelObject.transform.parent = chunk.transform;
phixelObject.transform.localPosition = new Vector3(chunkPos.x - Constants.CHUNK_SIZE / 2, chunkPos.y - Constants.CHUNK_SIZE / 2, -chunkPos.z);
//Add Sprite Renderer
phixelObject.textureRenderer = phixelObject.AddComponent<SpriteRenderer>();
//"create Texture_x" + phixelObject.dims.x.ToString() + "_y"+ phixelObject.dims.y.ToString()
phixelObject.tex = new Texture2D(phixelObject.dims.x, phixelObject.dims.y, TextureFormat.RGBA32, -1, false, true);
return phixelObject;
}
</code>
<code>public static PhixelObject Create(Chunk chunk, Vector3Int chunkPos, List<Vector2> verticies, List<Phixel> perimeterPhixels, Phixel[,] phixelMatrix, GameObject gameObject = null) { //Create create PhixelObject phixelObject = Create<PhixelObject>(gameObject); //this just creates a new GameObject and adds the script as a component //Set Transform phixelObject.transform.localScale = new Vector3(Constants.PIXEL_PER_METER, Constants.PIXEL_PER_METER, 1); phixelObject.transform.parent = chunk.transform; phixelObject.transform.localPosition = new Vector3(chunkPos.x - Constants.CHUNK_SIZE / 2, chunkPos.y - Constants.CHUNK_SIZE / 2, -chunkPos.z); //Add Sprite Renderer phixelObject.textureRenderer = phixelObject.AddComponent<SpriteRenderer>(); //"create Texture_x" + phixelObject.dims.x.ToString() + "_y"+ phixelObject.dims.y.ToString() phixelObject.tex = new Texture2D(phixelObject.dims.x, phixelObject.dims.y, TextureFormat.RGBA32, -1, false, true); return phixelObject; } </code>
public static PhixelObject Create(Chunk chunk, Vector3Int chunkPos, List<Vector2> verticies, List<Phixel> perimeterPhixels, Phixel[,] phixelMatrix, GameObject gameObject = null)
{
    //Create create
    PhixelObject phixelObject = Create<PhixelObject>(gameObject);
    //this just creates a new GameObject and adds the script as a component

    //Set Transform
    phixelObject.transform.localScale = new Vector3(Constants.PIXEL_PER_METER, Constants.PIXEL_PER_METER, 1);
    phixelObject.transform.parent = chunk.transform;
    phixelObject.transform.localPosition = new Vector3(chunkPos.x - Constants.CHUNK_SIZE / 2, chunkPos.y - Constants.CHUNK_SIZE / 2, -chunkPos.z);

    //Add Sprite Renderer
    phixelObject.textureRenderer = phixelObject.AddComponent<SpriteRenderer>();

    //"create Texture_x" + phixelObject.dims.x.ToString() + "_y"+ phixelObject.dims.y.ToString()
    phixelObject.tex = new Texture2D(phixelObject.dims.x, phixelObject.dims.y, TextureFormat.RGBA32, -1, false, true);


    return phixelObject;
}

(I replaced the manual Profile.BeginSample() calls with comments for readability and removed parts that are not profiled)

well as said, it can’t be different texture sizes since these are the same most of the time.
I havent completely ruled out GC issues either but I’ve changed classes to structs and reference type arrays to value type arrays to make it a lot easier to collect stuff.

In general it just seem really weird to me that the same Texture2D initialization for example takes 0.05ms 20 times in a row, before taking 40ms once or twice and then going back to 0.05ms.

So I hope anyone might know whats going on here,
or at least can give me some tips for finding the culprit.
Thanks in advance for any help! 🙂

New contributor

Obarmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật