JNI手动释放内存(避免内存泄露)
. 哪些需要手动释放?
不要手动释放(基本类型): jint , jlong , jchar
需要手动释放(引用类型,数组家族): jstring,jobject ,jobjectArray,jintArray ,jclass ,jmethodID
. 释放方法
2.1 jstring & char *
// 创建 jstring 和 char*
jstring jstr = (*jniEnv)->CallObjectMethod(jniEnv, mPerson, getName);
char* cstr = (char*) (*jniEnv)->GetStringUTFChars(jniEnv,jstr, );
// 释放
(*jniEnv)->ReleaseStringUTFChars(jniEnv, jstr, cstr);
(*jniEnv)->DeleteLocalRef(jniEnv, jstr);
2.2 jobject,jobjectArray,jclass ,jmethodID等引用类型
(*jniEnv)->DeleteLocalRef(jniEnv, XXX);
2.3 jbyteArray
jbyteArray audioArray = jnienv->NewByteArray(frameSize);
jnienv->DeleteLocalRef(audioArray);
2.4 GetByteArrayElements
jbyte* array= (*env)->GetByteArrayElements(env,jarray,&isCopy);
(*env)->ReleaseByteArrayElements(env,jarray,array,);
2.5 NewGlobalRef
jobject ref= env->NewGlobalRef(customObj);
env->DeleteGlobalRef(customObj);
. 避免内存泄露
JNI如果创建以上引用却不手动释放的话很容易就造成内存泄露,所以JNI编程创建的引用类型一定要手动释放(切身教训),检测内存泄露可以使用Jprofiler
JNI手动释放内存(避免内存泄露)的更多相关文章
- Linux内存机制以及手动释放swap和内存
今天我们来谈谈Linux的内存机制. 首先我们理一下概念 一.什么是linux的内存机制? 我们知道,直接从物理内存读写数据要比从硬盘读写数据要快的多,因此,我们希望所有数据的读取和写入都在内存完成, ...
- 手动释放linux内存cache
总有很多朋友对于Linux的内存管理有疑问,之前一篇linux下的内存管理方式似乎也没能清除大家的疑虑.而在新版核心中,似乎对这个问题提供了新的解决方法,特转出来给大家参考一下.最后,还附上我对这方法 ...
- 可用内存free不足 hadoop3 无法启动 手动释放缓存 cache
[root@hadoop3 hadoop]# xlfg total used free shared buff/cache availableMem: 15 0 2 0 12 14Swap: 7 0 ...
- 【转】手动释放linux os buff/cache
手动释放linux内存cache和脚本定时释放 标签: linuxcache脚本bufferwindows磁盘 2011-12-04 08:44 12799人阅读 评论(2) 收藏 举报 分类: l ...
- linux如何手动释放linux内存
当在Linux下频繁存取文件后,物理内存会很快被用光,当程序结束后,内存不会被正常释放,而是一直作为caching.这个问题,貌似有不少人在问,不过都没有看到有什么很好解决的办法.那么我来谈谈这个问题 ...
- Linux手动释放内存
手动释放内存 1.sync将内存中的缓存写入磁盘 2. to free pagecache, use echo 1 > /proc/sys/vm/drop_caches; to f ...
- Linux内存(手动释放cache)
项目的扩容申请了一台机器,到手之后看一下机器的指标,看到内存使用情况是这样的. 1.查看内存 free $ free -h total used free shared buffers cached ...
- Ubuntu系统下手动释放内存
有时候,像mongo这种,对内存只吃不吐的,我们要手动释放一下. drop_caches的详细文档如下:Writing to this will cause the kernel to drop cl ...
- 内存测试——内存泄露工具 LeakCanary
项目地址 https://github.com/square/leakcanary 接入方法 1. 配置依赖 dependencies { debugCompile 'com.squareup.lea ...
随机推荐
- mysql 汇总
子查询: select a.id,a.hotelname,max(b.day) as day,a.hotelrankid, c.hotelrank,min(b.basicprice) a ...
- kivy __init__() got an unexpected keyword argument '__no_builder' Kivy
from kivy.lang.builder import Builder from kivy.app import App, runTouchApp from kivy.uix.boxlayout ...
- 登陆 全站 user
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join( ...
- Prometheus监控学习笔记之在 HTTP API 中使用 PromQL
0x00 概述 Prometheus 当前稳定的 HTTP API 可以通过 /api/v1 访问. 0x01 API 响应格式 Prometheus API 使用了 JSON 格式的响应内容. 当 ...
- select2 AJAX获取数据
页面效果: index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...
- Bugku-CTF之web基础$_POST
Day4 web基础$_POST http://123.206.87.240:8002/post/ 本题要点: POST传参 打开发现这样一段代码
- centos7 update docker
yum erase docker docker-common docker-client docker-compose -y 编辑一个docker镜像源 vim /etc/yum.repos.d/do ...
- fatal: unable to access 'https://github.com/open-falcon/falcon-plus.git/': Peer reports incompatible or unsupported protocol version
git通过git clone下载github上的资源到机器上,结果出现如题所示的错误. [root@server data]# git clone https://github.com/pingcap ...
- maven项目更新之后,JDK版本成为1.5
描述:maven项目更新之后,JDK版本成为1.5? 解决:在pom.xml文件中配置java版本,选中build path 设置之后,刷新maven项目
- AndroidO Treble架构分析【转】
本文转载自:https://blog.csdn.net/yangwen123/article/details/79835965 从AndroidO开始,google引入了Treble架构,目的是为了方 ...