★ wanayoo — archive 1999 https://github.com/NativeScript/android-runtime/issues/1544Nouvelle recherche | Portail wanayoo
Skip to content
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

Runnable memory leak #1544

Open
patricklx opened this issue Nov 27, 2019 · 6 comments
Open

Runnable memory leak #1544

patricklx opened this issue Nov 27, 2019 · 6 comments
Labels
bug

Comments

@patricklx
Copy link

@patricklx patricklx commented Nov 27, 2019

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI:
  • Cross-platform modules: 6.2.1
  • Android Runtime: 6.2.0
  • iOS Runtime (if applicable):
  • Plugin(s):

Describe the bug

We noticed the memory increasing in our app, which is when we started to investigate memory leaks.
We noticed in Android Studio Profiler that Runnable is never deallocated and this also happens for
NativeScriptHashMap.HashMapEntry

Most entries happen when

  • an Object was initialised inside a Runnable
  • inside a worker thread
  • through an Intent Handler
addNewEntry:449, NativeScriptHashMap (com.tns)
put:380, NativeScriptHashMap (com.tns)
makeInstanceStrong:948, Runtime (com.tns)
initInstance:791, Runtime (com.tns)
<init>:12, Runnable (com.tns.gen.java.lang)
callJSMethodNative, Runtime (com.tns)
dispatchCallJSMethodNative:1286, Runtime (com.tns)
callJSMethodImpl:1173, Runtime (com.tns)
callJSMethod:1160, Runtime (com.tns)
callJSMethod:1138, Runtime (com.tns)
callJSMethod:1134, Runtime (com.tns)
run:17, Runnable (com.tns.gen.java.lang)
handleCallback:789, Handler (android.os)

Is it possible that com.tns.gen.java.lang.Runnable is overriding java.lang.Runnable? Since I'm expecting to see java.lang.Runnable, but I only see com.tns.gen.java.lang.Runnable

@patricklx
Copy link
Author

@patricklx patricklx commented Dec 6, 2019

turns out this was because the Runnable was calling a callback with a Collection. We where calling toArray on it (inside the callback). In android Studio I could see that it was not beeing released. Looks like anything thats alloacted within that callback is not released, since i also tested with an iterator. (though memory usaged is quite less)
So it was probably holding a reference to it.
This is still with the markingMode: full.
will test now with none

@darind
Copy link
Member

@darind darind commented Dec 9, 2019

@patricklx, could you please attach a sample project illustrating the issue? Also did you get a chance to test with markingMode: none?

@patricklx
Copy link
Author

@patricklx patricklx commented Dec 9, 2019

Hi @darind
I was able to create a sample app. I did test with markingMode: none, and it was working well.
But we are not yet ready to use it, since we would need additional testing with our custom plugins...

In this example you will be able to see that StringBuilder is leaking, so never released.

https://github.com/patricklx/ns-memory-leak
patricklx/ns-memory-leak@be91f12

@darind darind added the bug label Dec 10, 2019
@darind
Copy link
Member

@darind darind commented Dec 10, 2019

@patricklx, thanks for the sample project. This is indeed a bug with markingMode: full which is now deprecated in favour of markingMode: none. One workaround is to manually release the memory associated with those objects using the __releaseNativeCounterpart method after you have finished using them:

const Test = my.Test.Test.extend({
    invoke(arg) {
        const val = arg.toString();
        console.log('invoked', val.length);

        // Manually release the native memory retained by the parameters:
        __releaseNativeCounterpart(arg);
    }
});
@patricklx
Copy link
Author

@patricklx patricklx commented Dec 10, 2019

thanks, I already found that workaround. But it also does not release all other native object allocations in that callback... So, for every native function that returns a new object
Will you work on a fix or will you not fix this because of deprecation

@darind
Copy link
Member

@darind darind commented Dec 10, 2019

We are not planning on making changes to the markingMode:full algorithm as it is now deprecated.

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.