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

Remove NSObject.methodMap or make it thread-safe #34

Open
Marcono1234 opened this issue Jan 5, 2021 · 2 comments
Open

Remove NSObject.methodMap or make it thread-safe #34

Marcono1234 opened this issue Jan 5, 2021 · 2 comments

Comments

@Marcono1234
Copy link
Contributor

Marcono1234 commented Jan 5, 2021

The field NSObject.methodMap is of type HashMap and is accessed without any synchronization:

private static Map<Class,Map<String,Method>> methodMap = new HashMap<Class,Map<String,Method>>();

However, if access it correctly synchronized it would still be a memory leak because the map would constantly grow and is never cleared.
Would it maybe make sense to make this an instance field (i.e. remove static)? Though I am not very familiar with this project and what performance implications this would have.

@shannah
Copy link
Owner

shannah commented Jan 6, 2021

The method map serves as a sort of vtable for looking up the Java method to use for handling a given Objective-C method selector. Each subclass of NSObject will have a corresponding entry. Conceptually this is static with a one-to-one correspondence between a Java class and a method table.

To satisfy your concern about growing without bound, perhaps it should be changed to a WeakHashMap so that if the class is GC'd, its method table will be cleaned up.

@Marcono1234
Copy link
Contributor Author

Ah, right then it would likely not be a memory leak because there will only be a limited amount of classes. But using a WeakHashMap might be useful nonetheless to allow removal when a custom class loader which loaded the class is unloaded? However, that might make synchronization more complicated / inefficient.

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

No branches or pull requests

2 participants