- Home /
Memory leak but only in the executable
I am having an issue with a memory leak that only happens in the compiled game. I have tried using the memory profiler and it gives me the same memory usage for each capture. It is not showing the leak. I have also tried using the main profiler which shows Total system memory usage at 2.21G and growing but does not show it anywhere in the details. The game will crash after about 24 hours. I have done a lot of looking online but I have not found anything that might help me track this down. Is there another way to find what is causing this or at least point me to an area?
Update: So Since I am a noob at some of this I will add in what I have found for other noobs.(I think my assumptions are correct) in profiler I clicked on the memory square next to the graph. This displayed the simple stats where I can see that the 'Total System Memory Usage' is increasing. The last line shows 'GC Allocations per Frame'. I noticed this number seems quite large for the scene that is not currently doing anything(no animations or activity). It is running between 30k and 150k per frame. Next i clicked on the CPU Usage square at the top and in the bottom section sorted by GC Alloc and started drilling into the highest value which is giving me the routine that is generating all of the GC Allocation. Looking at the script I am not seeing the issue(so far). I changed a few places where I pass a string to pass as reference but that did not help. Still looking.
If any of this is incorrect on how to chase this down, please correct.
The process that i have done so far only pointed me to some code that I could have done better. In that script i was doing some string concatenation that was causing excessive GC. Fixing that reduced it considerably but it did not fix the leak. After going through the code for about seven hours I finally decided to do a side by side comparison with my development system and the production system. The development system is Windows 10 Ent. and the production is Windows 10 IoT. The Exe runs with no leaks on the dev machine but has a severe leak on the IoT machine. IoT is a enterprise with some stuff not installed and I have disabled some services and applied some group policies. At this point I assume that Unity wants something that isn't installed, a service that is disabled or some setting that I have changed. Next step i am going to try is a fresh IoT install.
Answer by bills801 · Aug 26, 2021 at 08:00 PM
Well, I Guess it isn't a Unity issue but it is the craziest thing i have come across in a while. If i start up windows and then click on the app, everything is fine. If I start the app using a task in task manager I get the memory leak. So far I have tried a few other ways to start the app on startup but none of them launch it.
If you're launching it through another program, then you're inheriting various permissions from that other program (Ad$$anonymous$$istrator privileges, etc.).
Do you have information available on the differences between permissions between launching your Unity game yourself, vs. the program you're launching it from?
After downloading and running Microsoft Debugdiag it is pointing to mono-2.0-bdwgc.dll. It is flagging mono_2_0_bdwgc!mono_marshal_string_to_utf16+beb. I have no idea if it is an issue in the dll or if it is how it is called from the games scripts. From what the report is saying is that this is what is leaking all of the memory.
Module Information
Image Name: E:\MonoBleedingEdge\EmbedRuntime\mono-2.0-bdwgc.dll Symbol Type: Export Base address: 0x00000003`00905a4d Time Stamp: Mon Apr 12 08:56:12 2021
Checksum: 0x00000000`00000000 Comments:
COM DLL: False Company Name:
ISAPIExtension: False File Description:
ISAPIFilter: False File Version:
Managed DLL: False Internal Name:
VB DLL: False Legal Copyright:
Loaded Image Name: mono-2.0-bdwgc.dll Legal Trademarks:
Mapped Image Name: Original filename:
Module name: mono_2_0_bdwgc Private Build:
Single Threaded: False Product Name:
Module Size: 7.43 MBytes Product Version:
Symbol File Name: mono-2.0-bdwgc.dll Special Build: &
Top 5 functions by allocation count
mono_2_0_bdwgc!mono_marshal_string_to_utf16+beb - 677,786 allocation(s) mono_2_0_bdwgc!GC_malloc_uncollectable+58cd4 - 2,659 allocation(s) mono_2_0_bdwgc!GC_malloc_uncollectable+58e0f - 1,167 allocation(s) mono_2_0_bdwgc!GC_malloc_uncollectable+7095 - 05 allocation(s) mono_2_0_bdwgc!mono_valloc+4c - 03 allocation(s)
Top 5 functions by allocation size
mono_2_0_bdwgc!mono_marshal_string_to_utf16+beb - 337.41 MBytes mono_2_0_bdwgc!GC_malloc_uncollectable+7095 - 4.98 MBytes mono_2_0_bdwgc!GC_malloc_uncollectable+58cd4 - 473.06 KBytes mono_2_0_bdwgc!mono_valloc+4c - 192 KBytes mono_2_0_bdwgc!GC_malloc_uncollectable+58e0f - 144.67 KBytes
Function details
Function mono_2_0_bdwgc!mono_marshal_string_to_utf16+beb Source Line
Allocation type OLE/COM allocation(s) Allocation Count 677786 allocation(s) Allocation Size 337.41 MBytes Leak Probability 100%
Well, this *does* seem to indicate that it's almost, if not entirely, the garbage collector that's leaking memory.
Based on your testing, hardware/platform dependency for the rate that memory leaks unfortunately lands this beyond the scope where I can offer useful advice.
This is more the sort of case where you will need to try and bring it to the developers' attention as best as possible, even if that means as little as a Bug Report.
Edit: More importantly, this is good information to get out there, so the more detailed analysis you can gather right now, the better.
I did a test where I set a 1 $$anonymous$$ute delay on the task and restarted. The game still leaked. Next I just right clicked on the task and told it to run. Still leaked. Ran the exe Directly. No leak. I then cleared some of the task settings and tried again. Game leaked. Unchecked Run with highest privileges and it doesn't run at all. When I installed Windows I forgot to set the Game to run as ad$$anonymous$$ and I get errors in debug. After setting the game to run as ad$$anonymous$$ I do not get those error in any attempted launch configuration.
Current settings: Game is set to run as Ad$$anonymous$$ for all users.
Task: General Tab: The Run user account is an Ad$$anonymous$$istrator, Run only when when user is logged on, Run with highest privileges, Configured for Windows 10
Triggers: Begin Task at logon, any user, Delay task 1 $$anonymous$$ute, enabled
Actions: Start a program, Game
Conditions: None
Settings: Allow task to be run on demand, Do not start new instance if running
Launch attempts: Starting from a batch file fails to launch, Starting from startup folder fails to launch, Starting from Registry Launches with leak, (HKLM:\software\wow6432node\microsoft\windows\currentversion\run), starting from powershell script attached to task launches with leak (Start-Process -Filepath "Game" -Verb RunAs), starting from Task launches with leak
I downloaded Microsofts Degugdiag and ran it on the game. From what I am able to tell it is mono-2.0-bdwgc.dll. For some reason i can't get my crash report to upload.
In the end, this is what i found. My app uses Sqlite with Unity. When connection to the database, either Sqlite or Unity is doing some manipulation with the path string. My configuration for deployment has three logical drives. C is for the OS, D is for Data and E is used for the Program. With the Unity app files in the root of the E: drive the app would leak. While messing around trying to make sense of this I tried launching the app from different locations. i tried a folder on the desktop, a folder in D: and they all worked. I then deleted the files from E and copied the folder on to E and viola, no leak. I have now run the app for a week with no leaking.
Answer by GeroNL · Aug 27, 2021 at 02:22 AM
Hello, Maybe you can check on the load session, like in a while, like instantiate, bust in particle or something like that.
And maybe you using preprocessor in wrong place/used, cause your case just in executable thing. reference : https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
Hope it help.
Your answer
Follow this Question
Related Questions
editor script memory leak 1 Answer
Reading Profiler Results 1 Answer
What exactly triggers Out of Memory for android? 0 Answers
Device.Present profiling 1 Answer
Seeing total allocated memory per-function in the profiler? 0 Answers