如何处理加载了JNA的库

编程入门 行业动态 更新时间:2024-10-17 19:34:26
本文介绍了如何处理加载了JNA的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用JNA通过以下方式加载本机库:

I'm using JNA to load a native Library using:

MyLibrary INSTANCE = (MyLibrary) Native.loadLibrary("mylibrary.so", MyLibrary.class);

现在,我要清理并处置该库.我已经读过dispose方法,但这是在类NativeLibrary上定义的,我应该怎么称呼它?

Now I want to clean up and dispose the library. I've read about the dispose method but this is defined on the class NativeLibrary, how I am supposed to call this?

无论如何,有必要这样做吗?我正在将jna与Apache Spark一起大规模使用,因此我正在加载该库数千次,我想知道是否如果我明确调用dispose,还有任何资源未打开?

Anyway, is it necessary to do that? I'm using jna with Apache Spark on a large scale, so I'm loading the library thousand of times, I'm wondering of there are any resources left open if I do nit excplicitly call dispose?

我已经看到了 Jna,从Java类中卸载Dll的问题动态,但无法解决我的问题.

I've seen the question Jna, Unload Dll from java class dynamically, but it does not provide a solution to my problem.

没有可接受的答案.人们建议调用NativeLibrary.dispose(),但是NativeLibrary中没有这样的静态方法.如果我尝试强制转换我的库实例(类型为Library),那么我将得到一个类广播异常.

There is no accepted answer. People suggest to call NativeLibrary.dispose(), but there is no such static method in NativeLibrary. If I try to cast my library instance (which is of type Library), then I get a class-cast exception.

推荐答案

您的帖子暗示您主要关注资源消耗.不必担心要成千上万次"加载它-它很像一个静态变量,一旦加载就不会重新加载.

Your post implies that you are concerned primarily with resource consumption. This is not a concern for loading it "thousands of times" -- it is much like a static variable, once loaded it is not reloaded.

如果您确实要卸载它,则基本上有三个选择.

If you do want to unload it, you essentially have three options.

选项1:

INSTANCE = null; System.gc(); // or simply wait for the system to do this

不能保证何时(如果有)将收集对象,但是通过将INSTANCE设置为null,可以允许系统在需要时回收其资源. (dispose()方法被称为对象的finalize()方法的一部分,因此在最终收集对象时会将其处置.)如果您真的完成了实例,那么这可能就足够了.请注意,在由于其他行为需要重新加载库的情况下,不应依赖此 ,因为不能保证垃圾回收.

There are no guarantees when (if ever) the object will be collected, but by setting INSTANCE to null, you allow the system to reclaim its resources when it wants to. (The dispose() method is called as part of the object's finalize() method, so it gets disposed when the object is eventually collected.) If you're really done with the instance, this may be sufficient for you. Note this should not be relied on in cases where reloading the library is necessary for other behavior, as garbage collection is not guaranteed.

选项2:

INSTANCE = null; NativeLibrary lib = NativeLibrary.getInstance("mylibrary.so"); lib.dispose();

这将显式处理本机库,如果需要重新加载库以在重新加载时强制执行程序行为(而不是重用现有实例),它将是首选方法.请注意,在加载库时是否使用了选项您还需要在此调用中使用这些相同的选项.

This will explicitly dispose of the native library, and would be the preferred method if you need to reload the library to force programmatic behavior on reloading (as opposed to reusing the existing instance.) Note if you used options when loading the library you need to use those same options with this call as well.

选项3:

NativeLibrary.disposeAll();

这将卸载所有库.根据您使用的其他库数量(再次使用时必须重新加载),这可能对您有用.

This unloads all your libraries. Depending on how many other libraries you use (which will have to be reloaded when used again) this may work for you.

请注意,在所有这些选项中,您可能需要在处理后会延迟几毫秒,以确保本机操作系统不会返回相同的句柄.

Note that in all these options you may want a small time delay of a few milliseconds after disposing, to ensure the native OS does not return the same handle.

更多推荐

如何处理加载了JNA的库

本文发布于:2023-11-26 23:00:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635556.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   加载   JNA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!