Search Unity

Question Is it normal that it does this much GC Alloc?

Discussion in 'Localization Tools' started by mrCharli3, Mar 3, 2023.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I am running at few thousand translations just for testing in the same frame, and it really hits hard on performance. Is it normal that it does this much GC alloc? Im on latest version. Would you suggest I cache the table?

    My code:
    Code (CSharp):
    1.  
    2. public static StringTable GetStringTable()
    3.     {
    4.         return LocalizationSettings.StringDatabase.GetTable("stringTable", LocalizationSettings.SelectedLocale);
    5.     }
    6.  
    7. public static string GetLocalizedString(string entryKey)
    8.     {
    9.         string result = "";
    10.  
    11.         try
    12.         {
    13.             result = GetStringTable().GetEntry(entryKey).GetLocalizedString();
    14.         }
    15.         catch
    16.         {
    17.             Debug.LogError("Could not find localization entry: " + entryKey);
    18.         }
    19.  
    20.         return result;
    21.     }
    upload_2023-3-3_10-5-4.png
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    What version are you using?
    We have improvements to 1.4.3.
    Are you doing any string formatting?
     
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    The latest version.
    I do a lot of string formating.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    String formatting does allocate. We have some improvements to this in the future (maybe 2.0) but there's no way to completely avoid it.
    What sort of formatting are you doing?
     
  5. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Oh had no idea, I use
    string.Format()
    .

    string.Format("Dynamic value: {0}", value)
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Ah yes that will allocate. It has to create a new string with the formatting applied. Is that the only allocation happening?
     
  7. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Ah thats annoying but makes sense. Might be yeah!
     
    karl_jones likes this.