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

Release native resources from Java via AutoCloseable #1040

Closed
oliviercailloux opened this issue Feb 1, 2019 · 7 comments
Closed

Release native resources from Java via AutoCloseable #1040

oliviercailloux opened this issue Feb 1, 2019 · 7 comments
Assignees
Labels
Help Needed Modeling/Usage problem Lang: Java Java wrapper issue
Projects
Milestone

Comments

@oliviercailloux
Copy link

Reading #100, it seems that by design OR-Tools does not provide a way to explicitly close solvers and release native resources created by the JNI calls. Finalizers are supposed to take care of this, AFAIU.

This is rather unusual in Java, and, if I dare to say, is usually considered incorrect. Bloch’s Effective Java explains it in detail in Item 8 (of the 3rd edition).

As a summary: “Finalizers are unpredictable, often dangerous, and generally unnecessary. Their use can cause erratic behavior, poor performance, and portability problems. Finalizers have a few valid uses, which we’ll cover later in this item, but as a rule, you should avoid them. (…) C++ programmers are cautioned not to think of finalizers or cleaners as Java’s analogue of C++ destructors.”

The normal mechanism is to implement AutoCloseable and let your users close resources when they know they don’t need them any more.

@lperron
Copy link
Collaborator

lperron commented Feb 1, 2019 via email

@lperron lperron closed this as completed Feb 1, 2019
@Mizux Mizux added Help Needed Modeling/Usage problem Lang: Java Java wrapper issue labels Feb 1, 2019
@Mizux Mizux added this to To do in Kanban 7.0 via automation Feb 1, 2019
@Mizux Mizux added this to the v7.0 milestone Feb 1, 2019
@oliviercailloux
Copy link
Author

Swig only does what the developer tells it to do. My arguments have not been answered or even considered.

@lperron
Copy link
Collaborator

lperron commented Feb 1, 2019 via email

@oliviercailloux
Copy link
Author

I am afraid I do not have a ready made solution for you… I only wanted to draw attention to this possible problem, not claim that I am ready to solve it. Solving it would require, I think, not huge time and effort, but still, probably some significant time and effort and some planning and thinking about a few API changes.

I am sure that at Google you do have very experimented developers in Java that will be able to help. (I am a happy user of Guava.)

@lperron
Copy link
Collaborator

lperron commented Feb 1, 2019 via email

@oliviercailloux
Copy link
Author

Indeed the SWIG doc itself suggests to leave the GC mechanism reclaim memory automatically. That runs exactly counter the advice of Bloch (which, I think, has good arguments to defend his point of view, whereas SWIG does not argue on that topic). Perhaps the SWIG people would consider Bloch too perfectionist on that topic? (Or perhaps they simply didn’t think about his arguments.) OTOH, LP is precisely a domain where performance and good memory management matter. I’d suggest you to read the item I cited in my first post and decide.

If you opt for explicit possibility of releasing resources, I think you simply have to add explicit method calls that release resources and that your users can call through a method called “close” (by convention and because it allows the object to implement AutoCloseable). I don’t think any of this would be special to SWIG, that would just be normal method calls through JNI (wrapped by SWIG, of course). Perhaps this can help? I don’t know much about SWIG so I may be wrong.

FWIW, CPLEX allows to close resources from Java. But as it’s not open source, I’m not sure how much you can learn from this. At least the high level API is available for study.

@lperron
Copy link
Collaborator

lperron commented Feb 1, 2019

Thanks for the insight.
Unless someone come with a compelling example that demonstrates this is the only solution, I will not complexify my swig code. This is already too much of a burden.

@lperron lperron moved this from To do to Done in Kanban 7.0 Feb 1, 2019
@Mizux Mizux self-assigned this Oct 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Needed Modeling/Usage problem Lang: Java Java wrapper issue
Projects
No open projects
Kanban 7.0
  
Done
Development

No branches or pull requests

3 participants