Skip to content

The library does not compile when used with IL2CPP in Unity3D #1009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
terjew opened this issue Jan 10, 2022 · 11 comments
Closed

The library does not compile when used with IL2CPP in Unity3D #1009

terjew opened this issue Jan 10, 2022 · 11 comments
Labels

Comments

@terjew
Copy link

terjew commented Jan 10, 2022

Describe the bug
When using the library in Unity3D and compiling the Unity3D project for android 64-bit using IL2CPP, the clang compiler exits with an out of memory error, after consuming around 30GB virtual memory.

To Reproduce
Steps to reproduce the behavior (just an example):

  1. Added nuget UnitsNet 4.110.0 to a .NET Standard 2.1 project
  2. Build using Visual Studio 2022
  3. Add the resulting library, as well as the UnitsNet dll, to a Unity3D project, using Unity version 2021.2.4f1 and create a script component using the library from step 2, adding it to the scene.
  4. Set up the project to build for Android 64-bit with IL2CPP
  5. Build the project
  6. Observe that clang.exe errors out, after consuming around 30 GB of virtual RAM.

Expected behavior
The unity project should build without errors.

Additional context
I believe the sheer amount of types and methods in the project might be causing the problem, especially if IL2CPP places everything in a single compile unit. Maybe an effort to split up the library by categories, measurement systems or similar as discussed in #372 could help.

@terjew terjew added the bug label Jan 10, 2022
@angularsen
Copy link
Owner

Huh, interesting! 😅
Seems like a hardware problem to me, you just need more memory 😂

To be honest, I don't think UnitsNet is a good fit for Unity3D. I'm sure we could figure out the IL2CPP problem, but you are bringing in about 2MB of binary just for some help with conversions. Also, performance is not a priority in this library, which I would typically think is a concern in games and live rendering.

I don't personally have the time to look into this, but if you want to take a look and try to debug it and maybe come up with a pull request, then I'd be happy to assist and help get it merged.

@terjew
Copy link
Author

terjew commented Jan 10, 2022

Well, since it's only for the build, I guess adding more RAM would theoretically be fine... 😅

Apart from that though, I found it a pretty good fit for my needs. It did not get in the way performance-wise, neither when I tried it on desktop in Unity or in Blazor(!) in another project using the same assembly. The 2MB is definitely something I would have liked to change, however, as I am only using a tiny part of the library.

I'm afraid the time schedule is pretty tight in the current project, so my current solution was to simply yank it out, but if I have more time at a later point I might have another look.

@tmilnthorp
Copy link
Collaborator

@joshpeterson I don’t suppose you could offer any insight?

@joshpeterson
Copy link

@joshpeterson I don’t suppose you could offer any insight?

Hey @tmilnthorp! 👋

Yes, I might be able to help a bit.

if IL2CPP places everything in a single compile unit

IL2CPP will split the generated code across multiple translation units (.cpp files), even within one managed assembly. Still, it is possible that something is too large for the C++ compiler for Android. We see a number of issues like this with the Android NDK tools specifically.

In Unity 2022.1 and later we have an IL2CPP Code Generation option available in the Player Settings that will allow you to tell IL2CPP to favor smaller executable code size. That might help - it really varies per project though.

Sorry that I can't provide a more definitive answer.

@angularsen
Copy link
Owner

Thanks a lot for chiming in @joshpeterson , I guess the Unity option is something to try then @terjew .
I will close this issue until someone wants to take a stab at fixing it on our end somehow.

@tmilnthorp
Copy link
Collaborator

Hey @joshpeterson! 👋 Thanks for the info.

@terjew I wonder if #1018 would magically fix this issue also? 🤞

@angularsen
Copy link
Owner

@terjew Nuget should be out shortly, would you mind giving it a try and report back?

Release UnitsNet/4.115.0 · angularsen/UnitsNet

@terjew
Copy link
Author

terjew commented Jan 26, 2022

@angularsen @tmilnthorp I'll try to find time to test it tomorrow, will report back.

@terjew
Copy link
Author

terjew commented Jan 30, 2022

@angularsen @tmilnthorp I just finished testing now. With the version I originally used (4.110.0) I still get the out of memory error from clang. When updating to the latest nuget version (4.116.0) the compilation finishes without errors. It seems the latest fixes solved this issue 👍

@tmilnthorp
Copy link
Collaborator

tmilnthorp commented Jan 30, 2022

@angularsen @tmilnthorp I just finished testing now. With the version I originally used (4.110.0) I still get the out of memory error from clang. When updating to the latest nuget version (4.116.0) the compilation finishes without errors. It seems the latest fixes solved this issue 👍

Great news! I had a nagging feeling that method size was related. Glad it works.

@joshpeterson FYI fixed with #1018

@angularsen
Copy link
Owner

Great! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants