Hi all,
while converting my project by using J2ObjC 2.4, I realized following behavior by using the Instruments tool on the Mac.
Unless in Java the converted application never diposes my Threads and still shows the following "Created & Persistent" Allocations in the Instruments tool:
Here in this picture you can see that all the Threads which got started within my SWIFT demo application are getting kept as "Created & Persistent" allocations.
I created a very basic Thread example in Java and used the following converted classes in my demo application:
SimpleThread.java
package com.test.thread;
public class SimpleThread extends Thread {
private final static int NUMBER_OF_ITERATIONS = 10000;
public void run() {
addSomething();
}
private int addSomething() {
int result = 0;
for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
result += i;
}
return result;
}
}
SimpleThreadExecutor.java
package com.test.thread;
public class SimpleThreadExecutor {
private SimpleThread simpleThread;
public void startSimpleThread() {
this.simpleThread = new SimpleThread();
this.simpleThread.start();
}
}
ViewController.swift
class ViewController: UIViewController {
@IBAction func buttonClicked (sender: AnyObject) {
let simpleThreadExecutor : ComTestThreadSimpleThreadExecutor = ComTestThreadSimpleThreadExecutor.init();
simpleThreadExecutor.startSimpleRunnable();
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
I already experimented with ARC and Non-ARC mode but actually both of them are resulting in the same problem.
In my understanding of the shown allocations in the instruments tool this would lead to a memory leak as Threads will never get disposed until the application ends.
Can someone please clarify how to properly use Java Threads with J2ObjC, I guess I am doing something fundamentally wrong here.
Thank you:)