Fix memory leak caused by NSLocalizedString #891
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I detected very high memory usage on some screens of my app. I investigated the problem and found that the problem is
NSLocalizedString(_:tableName:bundle:value:comment:)
calls on R.swift.This PR replaces
NSLocalizedString(_:tableName:bundle:value:comment:)
calls withString.init(localized:table:bundle:locale:comment:)
on supported platforms.Benchmarks
Steps
MyStrings.string
Strings File (Legacy)MyStrings.string
tomy_key = "My Text";
ViewController.swift
to code below.ViewController.swift
and SPM package repo/commit to enable different scenariosResults
NSLocalizedString(_:tableName:bundle:value:comment:)
High is 52.5 MB. It was rising steadily until the timer is stopped.
String.init(localized:table:bundle:locale:comment:)
High is 30.5 MB. It was 30 MB from the start to the end.
R.string.myStrings.my_key()
formac-cain13/R.swift master 8d26021c6c71a513505e722f2cc82a6ad4f7f087
High is 57.3 MB. It was rising steadily until the timer is stopped.
R.string.myStrings.my_key()
forfthdgn/R.swift nslocalizedstring-memory-leak 9f8f893d35f1d21e64abb8d991243099e3d5cdf0
High is 30.4 MB. It was around 30 MB from the start to the end.
SwiftUI
SwiftUI has similiar results.
Text.init(_:tableName:bundle:comment:)
uses around 24 MB.Text(_:)
with inputNSLocalizedString(_:tableName:bundle:value:comment:)
's memory usage increases steadily.Text(_:StringResource)
from R.swift on master branch behaves likeNSLocalizedString
and memory usage keeps increasing.Text(_:StringResource)
from R.swift on this PR branch uses around 24 MB