Just by having a second camera causes GC kick in. URP

I use camera stacking for my project. I have a second camera (for UI and other things that needs to get drawn over) that was set as an overlay camera in the stack.

I figured out what causes the GC. It is just simply having a second camera. Stacked or not, base or overlay does not matter. Strange thing is, it doesn’t even need to render anything. I turned off or disable everything on the camera and yet still GC shows up. Just being there is all it takes.

This is an empty project with the default URP work bench scene.

Am I doing something wrong here, how do I fix this?

Take a look at the screenshot.

I also did a bug report.

Hey! Can you share your bug report number please?

@erikabar

Here you go: Case 1262797

I already got two responses back. One, pointing to this (https://issuetracker.unity3d.com/issues/2d-renderer-using-more-than-one-camera-that-renders-out-to-a-render-texture-creates-gc-alloc-every-frame) and stating that this is fixed for Unity 2020 and URP 9.x.x. This does not help those who have 2019.4 LTS and URP 7.x.x.

Second, saying they opened “a backport case to the 2019.4 stream and hopefully:eyes:, it will be fixed in a separate package for the 2019.4 URP line (7.x.x)”.

Which is great but the world ‘hopefully’ worries me though and makes me wonder if there is a chance that this would not get fixed for those with 2019.4 and 7.x.x. Which would make it impossible to use the camera stack or multiple cameras in your project without significant performance consequences, especially for those with mobile or VR.

I will be happy to help test/troubleshoot any new packages, let me know!

Thank you.

Hello! I’ve had a separate thread open about this issue and entered that initial bug - it seems that half of the issue has been fixed, but the other half has not. I’m glad that someone else is experiencing this problem as I’ve been a little surprised that no one else has reported this as an issue!

1 Like

@FlightOfOne thanks! I just checked the bug report and it seems like it was already fixed and backported to URP 7.4 and 8.2, here’s the PR: [9.x.x] Fix allocation caused when sorting cameras in URP. by phi-lira · Pull Request #268 · Unity-Technologies/Graphics · GitHub
Could you check if your issue is solved on Unity 2019.4 and URP 7.4.1?

@Unusual-Cadence I will get get back to you once I get the update! :slight_smile:

1 Like

Thank you! There will still be GC every frame in 2019.4 and URP 7.4.1 - SortCameras does not allocate GC, but GetCameras does when you have more than one camera.

It is indeed fixed! Not only that this also fixed an issue where material previews in the project view was not showing just -always showed black.

For a second there I thought my highly sophisticated, evil master plan to take over the VR world that entirely relied on camera stacking would crumble down to the ground, one spike at a time :smile:.

Phew! I feel better now! You guys are awesome! thank you!!!

1 Like

I am not doing that thing (3 cameras outputting to one texture) you are doing with the render texture but I can confirm that I am not getting GC at all now. Have you tried outputting from only one camera to one render texture?

Good news that you’re no longer having this problem! For me, any combination of more than one camera (whether it’s base, overlay, rendertexture doesn’t seem to matter) creates GC allocs in the GetCameras() function called by the RenderPipelineManager for me, even with URP 7.4.1 and 2019.4.4. What does your camera setup look like, out of interest?

isn’t this an internal call?

Depends on the scene I am in, but mostly its 1 base camera + one stacked camera for UI + another stacked for VR hands.

So if you were to start a brand new clean/empty project (with latest Unity 2019.4 and 7.41), and a scene that has only 2 cameras and nothing else, you still get GC?

Yes - creating a new project with the Universal Render Pipeline project and adding a new camera to the scene shows up the issue unfortunately.

If you want to upload a zip file with that empty project I can take a quick look and try it on mine and see if it happens.

Hello! Check out this project, it’s about as basic as I could make to recreate the problem. One scene, two cameras, GC Allocs in the profiler: http://www.unusualcadence.com/bugtest/URPCameraGCAlloc.zip

6110060--665240--urp gc alloc.png

I tested it out and yes there was GC alloc! Then, I removed your URP and reinstalled it 7.4.1 and then the GC disappeared. Maybe there was something left over from 7.3.x, just remove and reinstall and it should fix the issue.

Hm! I’ve tried uninstalling and reinstalling the URP package in that test project and still get the GC allocs, nothing changes for me whatsoever. Are you sure uninstalling didn’t remove the scriptable pipeline setting from the Graphics / Quality menu?

All I did was remove and reinstall from the package manager.

Could you do me a massive favor and maybe share a test project where you have multiple cameras with no GC allocs? I’m seeing something odd in the frame debugger where draw calls from separate cameras are appearing under one ‘Render Camera’, and I’m not sure if that’s intended behavior or not!

Sure, I will later today.

@Unusual-Cadence @erikabar

So here is the thing. I was trying it out again as I was getting you the project and gc alloc showed up again! I then deleted the library, reopen and let everything reimport again -everything was fine, no gc. Then I tried on my main project and even that had the issue. Then I closed and reopened it and gc problem was no longer there… It seems to show up time to time and I just can’t figure out what caused it. Then it was gone.

I am only guessing here but some thing causes this, probably because of something we are doing (to cameras?) and when you do a reimport () it fixes it.

@erikabar Is it possible for you to share with us what the cause and what the fix you guys did? is this somehow shader/material related? This way, instead of blindly trying everything, we can to narrow our focus to a certain area when we try to troubleshoot this?

@Unusual-Cadence attached is the project and it does not have the GC issue, well at least when I tested it. Also, fyi -I deleted the library folder to reduce the size of this attachment and it might take extra time to rebuild/reimport.

If you do manage to get the gc alloc again maybe share it again with Unity, so they can have a look at it? I hope we get to the bottom of this.

6122561–667223–URPGCALLOCTEST.zip (172 KB)