rb90
05/04/2021, 10:33 AMMartins Iroka
05/05/2021, 9:03 AMlouiscad
05/05/2021, 9:05 AMrb90
05/05/2021, 9:12 AMlouiscad
05/05/2021, 9:16 AMrb90
05/05/2021, 2:47 PMfunc testFunc() {
let testArray = [UcLoginRequests(), UcLoginRequests(), UcLoginRequests(), UcLoginRequests(), UcSignupRequests(), UcSignupRequests(), UcSignupRequests(), UcSignupRequests(), UcSignupRequests()]
}
That function is inside a viewModel. It is called from a viewController. After that viewController is closed and removed from stack, also the associated viewModel is gone from the memory. But unfortunately this classes are still in the memory:louiscad
05/05/2021, 3:02 PMtestFunc
Swift function that creates an array out of Kotlin objects, and the Kotlin objects stay in memory long after? How long after do you check the memory contents? Also, do you have the debugger connected all that time? I know it can delay GC on Android, I don't know if the same is true for iOS with Kotlin/Native.rb90
05/05/2021, 3:09 PMIf I understand correctly, you call thatthat's right 👍 I check the memory by debugging the memory graph in xcode. I started that 1-2 minutes after the viewModel and the viewController calling that function are gone. And the elements are still present in the memory as you can see 😞. I would expect them to not be removed from memory like the viewController and the viewModel. I cannot understand why they are not garbage collected when the parent classes are not there anymoreSwift function that creates an array out of Kotlin objects, and the Kotlin objects stay in memory long after?testFunc
louiscad
05/05/2021, 3:11 PMclass WIllILeak(val justSomeRandomText: String)
rb90
05/05/2021, 3:29 PMfunc testFunc() {
let testArray = [ErrorObject(errorMessage: "hello", errorStacktrace: "a stacktrace", errorType: .dataMissingInResponseError, statusCode: nil), ErrorObject(errorMessage: "hello", errorStacktrace: "a stacktrace", errorType: .dataMissingInResponseError, statusCode: nil), ErrorObject(errorMessage: "hello", errorStacktrace: "a stacktrace", errorType: .dataMissingInResponseError, statusCode: nil), ErrorObject(errorMessage: "hello", errorStacktrace: "a stacktrace", errorType: .dataMissingInResponseError, statusCode: nil), ErrorObject(errorMessage: "hello", errorStacktrace: "a stacktrace", errorType: .dataMissingInResponseError, statusCode: nil)]
}
ErrorObject
is a simple data class....
I also add now a simple class into my module and will try that - it takes a few minutes the build 😁 - I do not use the mono repo structure...class TestClass(val justSomeRandomText: String) { }
my test function in the viewModel:
func testFunc() {
let test = TestClass(justSomeRandomText: "bla")
let testArray = [TestClass(justSomeRandomText: "bla"), TestClass(justSomeRandomText: "bla"), TestClass(justSomeRandomText: "bla"), TestClass(justSomeRandomText: "bla"), TestClass(justSomeRandomText: "bla"), TestClass(justSomeRandomText: "bla"), TestClass(justSomeRandomText: "bla")]
}
And same behaviour, viewModel and viewController removed from the memory but the kotlin class instances still there:louiscad
05/05/2021, 4:55 PMrb90
05/05/2021, 6:24 PMlouiscad
05/05/2021, 6:27 PMrb90
05/05/2021, 6:28 PMlouiscad
05/05/2021, 6:29 PMrb90
05/05/2021, 6:30 PMlouiscad
05/05/2021, 6:30 PMrb90
05/05/2021, 6:32 PM@IBAction func myAction(_ sender: Any) {
viewModel?.testFunc()
}
louiscad
05/05/2021, 6:35 PMList(100_000) { WillILeak("Hello " + Random.nextInt()) }
rb90
05/05/2021, 6:37 PMfunc testFunction() {
var array = [TestClass]()
for index in 0..<100000 {
array.append(TestClass(justSomeRandomText: "abc"))
}
}
Memory graph:multiplatform-swiftpackage
packs an *.xcframework
file. I did that a few minutes ago on my own with xcodebuild command without multiplatform-swiftpackage
. And the result is the same. The classes are causing leaks in a project where the generated framework is added (added manually the *.xcframework
without swift package manager and multiplatform-swiftpackage
).... It seems that this problem occurs when the kotlin classes are packed in an *.xcframework
😞. Then this leaks happen 😢.- ./gradlew clean
- ./gradlew build
- ./gradlew packForXcode
Putting that *.framework
in an app (like f.e. described here in the docs https://kotlinlang.org/docs/mobile/integrate-in-existing-app.html#create-a-shared-module-for-cross-platform-code) cause exactly same issues - so the *.xcframework
file seems not to be the problem when *.framework
behaves exactly the same 😞testFunc()
when playing around with the monorepo project. In the monorepo project the bahviour is exactly the same - there are memory leaks:present segue
)
• NextViewController has a viewModel dependency
• this viewModel dependency has this test func:
func testFunc() {
var array = [TestClass]()
for index in 0..<100000 {
array.append(TestClass(justSomeRandomText: "abc"))
}
}
• SecondViewController has an action ("My Action")
• inside @IBAction method viewModel?.testFunc()
is triggered
• after that close Button is clicked which dismisses "NextViewController"
-> when this vc and the viewModel is gone I would expect that also the memory is cleanup and there are no references anymore of the instantiated array containing TestClass
objects:louiscad
05/05/2021, 10:19 PMsvyatoslav.scherbina
05/06/2021, 7:20 AMkotlin.native.internal.GC.collect()
to force unused memory to be reclaimed.rb90
05/06/2021, 8:27 AMlouiscad
05/06/2021, 8:31 AMrb90
05/06/2021, 8:33 AMlouiscad
05/06/2021, 6:24 PMrb90
05/06/2021, 8:33 PMsvyatoslav.scherbina
05/07/2021, 8:40 AMI guess they would be removed after some memory pressureYes.
rb90
05/07/2021, 1:55 PM