博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unload Java JNI DLL
阅读量:6328 次
发布时间:2019-06-22

本文共 3447 字,大约阅读时间需要 11 分钟。

The problem

Once a dll is loaded in a java program using either System.load() or System.loadLibrary(), how do we unload it? Theoretically the dll should be unloaded when the corresponding class loader is being garbage collected.

When a class loader is garbage collected?

A class loader is garbage collected when:

  • All objects of classes loaded by the class loader should be garbage collected.
  • All references pointing to the class loader object should be null.

What class loader should we use?

It is not possible to use the default class loader because it won't be garbage collected. So, we should create our own class loader and use it instead of the default one.

Check these links for custom java class loaders implementations:

DLL unloading example using the custom class loader

Create a file A.java and add the following code.

package com.codethesis.example;public class A {    public A() {}    static {        System.loadLibrary("mydll");    }    public native void print();    public void finalize() {        System.out.println("A garbage collected");    }}

You'll need to have a JNI dll mydll.dll in the classpath which contains a method print() as shown in the code. Then create a file Test.java and add this code:

package com.codethesis.example;import java.lang.reflect.Method;public class Test {    public static void main(String[] args) throws Exception {        CustomClassLoader cl = new CustomClassLoader();        Class ca = cl.findClass("com.codethesis.example.A");        Object a = ca.newInstance();        Method p = ca.getMethod("print");        p.invoke(a);        p = null;        ca = null;        a = null;        cl = null;        System.gc();    }}

After System.gc() is called the custom class loader will be garbage collected and the dll wlll be unloaded.

 

Why do we use java reflection?

Isn't it possile just to cast the "a" object to type "A" as it is in the code below?

package com.codethesis.example;import java.lang.reflect.Method;public class Test {    public static void main(String[] args) throws Exception {        CustomClassLoader cl = new CustomClassLoader();        Class ca = cl.findClass("com.codethesis.example.A");        A a = (A)ca.newInstance(); //a ClassCastException is thrown!        a.print();        ca = null;        a = null;        cl = null;        System.gc();    }}

Actually, it is not possible because A class is loaded by the default class loader and therefore these are two different classes. However there is a simple solution for this problem using interfaces. Create a file IA.java and add the following code inside:

package com.codethesis.example;public interface IA {    public void print();}

Then modify the A class to implement this interface:

package com.codethesis.example;public class A implements IA {    public A() {}    static {        System.loadLibrary("mydll");    }    public native void print();    public void finalize() {        System.out.println("A garbage collected");    }}

The Test class should look like this:

package com.codethesis.example;public class Test {    public static void main(String[] args) throws Exception {        CustomClassLoader cl = new CustomClassLoader();        Class ca = cl.findClass("com.codethesis.example.A");        IA ia = (IA)ca.newInstance();        ia.print();        ca = null;        ia = null;        cl = null;        System.gc();    }} 来源:http://www.codethesis.com/

转载于:https://www.cnblogs.com/beautiful-scenery/p/3588834.html

你可能感兴趣的文章
谈谈AppDelegate
查看>>
揭秘OSS实战优化、UDF追求极致之路
查看>>
不和HR谈一次恋爱,你都不知道有多好
查看>>
码栈开发手册(五)---可视化方式开发(模块详解--流程)
查看>>
myrocks记录格式分析
查看>>
PostgreSQL、Greenplum DML合并操作 最佳实践
查看>>
阿里云人工智能科学家闵万里犀利点评:现在的人工智能有点过热
查看>>
Spark修炼之道(高级篇)——Spark源码阅读:第十节 Standalone运行模式解析
查看>>
移动应用要如何埋点上传才能收集更多数据?
查看>>
MIDle生命周期详解,以及工作原理
查看>>
Nginx + Shiro + Ehcache 实现负载均衡集群(成绩报告查询系统)
查看>>
Java CRC32的用法
查看>>
Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!
查看>>
【 MAKEFILE 编程基础之一】详细介绍MAKEFILE概念和其机制用途;
查看>>
C#几个经常犯错误汇总
查看>>
Java 三大特性之继承
查看>>
虚拟机10:使用U盘及U盘启动
查看>>
MySQl的几个配置项
查看>>
linux命令学习——tar
查看>>
UIWebView清除缓存和cookie[转]
查看>>