续jna的坑后,再来谈谈 finalize()怎么用

本文探讨Java中垃圾回收机制与finalize()方法的使用,解释对象不一定被回收的原因,强调finalize()方法的局限性及如何解决内存泄漏问题。通过System.gc()与System.runFinalizersOnExit()方法的介绍,揭示Java内存管理的复杂性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先你得知道:

java提供finalize()方法,垃圾回收器准备释放内存的时候,会先调用finalize()

      (1).对象不一定会被回收。

       (2).垃圾回收不是析构函数。

       (3).垃圾回收只与内存有关。

       (4).垃圾回收和finalize()都是靠不住的,只要JVM还没有快到耗尽内存的地步,它是不会浪费时间进行垃圾回收的。


finalize 一般是用来处理,非java资源,比如在一个java class里调用了 C的malloc 申请资源,就需要对应的free。 这个free就可以放在 finalize()中

但这一切依然是不可以靠的, 在jna的坑中我们知道, 某java对象memory 可以用jni native 方法malloc申请很多 C对象 从而占据大量的 java进程中 native heap的 内存,但是这部分内存是不占据jvm heap的,导致java Gc线程认为jvm heap足够 无需浪费资源去 做Gc,导致 memory 无法调用 finalize()从而无法释放 native heap中的C代码对象。导致一定程度的内存泄漏。


如何解决?

看来只有自己显式 去调jni 去free了

可以参考 nio中 利用PhantomReference去知道对象状态,从而显示释放 线程的内核 内存。



再总结多说一句,finalize 实在不那么靠谱。 基本可以放弃使用


System.gc 是不会强制命令 jvm去做gc的 也只是建议。

gc没有渠道,可以 人为显式精确触发

System.runFinalizersOnExit() 方法部分地解决了这个问题,但是是process 退出时 回收。refer to http://blog.youkuaiyun.com/carolzhang8406/article/details/6705831


注:比较匆忙比较乱 有缘者看之



Features Automatic mapping from Java to native functions, with simple mappings for all primitive data types Runs on most platforms which support Java Automatic conversion between C and Java strings, with customizable encoding/decoding Structure and Union arguments/return values, by reference and by value Function Pointers, (callbacks from native code to Java) as arguments and/or members of a struct Auto-generated Java proxies for native function pointers By-reference (pointer-to-type) arguments Java array and NIO Buffer arguments (primitive types and pointers) as pointer-to-buffer Nested structures and arrays Wide (wchar_t-based) strings Native long support (32- or 64-bit as appropriate) Demo applications/examples Supported on 1.4 or later JVMs, including JavaME (earlier VMs may work with stubbed NIO support) Customizable marshalling/unmarshalling (argument and return value conversions) Customizable mapping from Java method to native function name, and customizable invocation to simulate C preprocessor function macros Support for automatic Windows ASCII/UNICODE function mappings Varargs support Type-safety for native pointers VM crash protection (optional) Optimized direct mapping for high-performance applications. COM support for early and late binding. COM/Typelib java code generator. Community and Support All questions should be posted to the jna-users Google group. Issues can be submitted here on Github. When posting to the mailing list, please include the following: What OS/CPU/architecture you're using (e.g. Windows 7 64-bit) Reference to your native interface definitions (i.e. C headers), if available The JNA mapping you're trying to use VM crash logs, if any Example native usage, and your attempted Java usage It's nearly impossible to indicate proper Java usage when there's no native reference to work from. For commercial support, please contact twalljava [at] java [dot] net. Using the Library Getting Started Functional Description. Mapping between Java and Native Using Pointers and Arrays Using Structures and Unions Using By-Reference Arguments Customization of Type Mapping Callbacks/Function Pointers/Closures Dynamically Typed Languages (JRuby/Jython) Platform Library Direct Method Mapping (Optimization) Frequently Asked Questions (FAQ) Avoiding Crashes Primary Documentation (JavaDoc) The definitive JNA reference is in the JavaDoc. Developers Contributing to JNA Setting up a Windows Development Environment Setting up an Android Development Environment Setting up a RaspberryPi Development Environment Setting up a Mac Development Environment Releasing JNA Publishing to Maven Central Contributing You're encouraged to contribute to JNA. Fork the code from https://github.com/java-native-access/jna and submit pull requests. For more information on setting up a development environment see Contributing to JNA. If you are interested in paid support, feel free to say so on the jna-users mailing list. Most simple questions will be answered on the list, but more complicated work, new features or target platforms can be negotiated with any of the JNA developers (this is how several of JNA's features came into being). You may even encounter other users with the same need and be able to cost share the new development. License This library is licensed under the LGPL, version 2.1 or later, and (from version 4.0 onward) the Apache Software License, version 2.0. Commercial license arrangements are negotiable. NOTE: Oracle is not sponsoring this project, even though the package name (com.sun.jna) might imply otherwise.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值