当前位置: 首页 > news >正文

王也微信头像优化关键词怎么做

王也微信头像,优化关键词怎么做,新产品开发的5个步骤,网站的建设公司做安卓jni相关开发的总会在涉及到jni变量释放时怀疑人生,what? where? when? who? why? how? how much? 最近碰到一个比较奇怪的问题,有一个jni方法的耗时在随着调用次数的增加而上涨,但是没有明显的内存泄漏,经过我缜密分…

做安卓jni相关开发的总会在涉及到jni变量释放时怀疑人生,what? where? when? who? why? how? how much?

最近碰到一个比较奇怪的问题,有一个jni方法的耗时在随着调用次数的增加而上涨,但是没有明显的内存泄漏,经过我缜密分析之后,终于解决了深埋多年的疑惑。代码如下:

void HENativeUtils::vectorFloatToJArray(JNIEnv* env, const std::vector<float>& src, jobject obj, jfieldID fieldId)
{jfloatArray jArray = ( jfloatArray )env->GetObjectField(obj, fieldId);if (!jArray || env->GetArrayLength(jArray) != src.size()){jArray = env->NewFloatArray(src.size());env->SetObjectField(obj, fieldId, jArray);}jfloat* array = env->GetFloatArrayElements(jArray, nullptr);std::copy(src.begin(), src.end(), array);env->ReleaseFloatArrayElements(jArray, array, 0);
}

这个方法提供了对一个java对象obj中的float[]成员变量进行操作的功能,如果该对象为空或者size与需要被设置的对象size不一致则创建一个新的float[]并覆盖该对象。从上面代码可知我在使用完成后已经调用env->ReleaseFloatArrayElements将对应的jni数组释放,为什么还存在泄漏?甚至有动手能力比较强的小伙伴如果把这段代码复制到自己的jni代码中去调用,可能也不会有泄漏。

关于类似这种jfloatArray/jintArray/jbyteArray等等对象什么时候需要调用env->ReleaseFloatArrayElements很多稍微有点经验的小伙伴都知道,但是关于什么时候需要调用env->DeleteLocalRef,相信很多人都会比较模糊。

上面这段代码之所以存在泄漏,关键在于调用环境的差异。

当我们从java线程中调用cpp代码,这时候每个jni方法都会带一个JNIEnv*,这个JNIEnv就代表了这个java线程,在这个jni方法中调用上面的方法就很正常,因为这个jni会在方法结束后自动DetachCurrentThread,这个自动调用相当关键,就会自动清理掉jni中类似jfloatArray/jintArray/jbyteArray的局部变量。

相对应的,还有一种情况就是我们在cpp中创建的线程,当我们想在该线程中调用java的方法,通常会调用JavaVm的AttachCurrentThread来为当前线程获取一个JNIEnv*,并且在一条长时间运行的后台线程中只要我AttachCurrentThread并获取JNIEnv*之后我就可以一直使用这个JNIEnv*来调用java方法。这个时候就很容易出问题了,因为这个线程的生命相当长,而我们每次在这个线程中调用方法vectorFloatToJArray时都会有一个新的局部变量jfloatArray,在我们自己创建的回调方法中没有自动的DetachCurrentThread,所以这个变量就泄漏了。值得注意的是,如果存在cpp线程->java方法->jni方法,此时这个jni方法虽然看起来长得和从java线程调过来的方法一模一样,但是相差甚远的是其JNIEnv*代表的其实还是前面AttachCurrentThread所获得的,如果之前没有手动调用过DetachCurrentThread,这里也一样会泄漏。

上面的方法保险起见应该加上一行env->DeleteLocalRef()

void HENativeUtils::vectorFloatToJArray(JNIEnv* env, const std::vector<float>& src, jobject obj, jfieldID fieldId)
{jfloatArray jArray = ( jfloatArray )env->GetObjectField(obj, fieldId);if (!jArray || env->GetArrayLength(jArray) != src.size()){jArray = env->NewFloatArray(src.size());env->SetObjectField(obj, fieldId, jArray);}jfloat* array = env->GetFloatArrayElements(jArray, nullptr);std::copy(src.begin(), src.end(), array);env->ReleaseFloatArrayElements(jArray, array, 0);env->DeleteLocalRef(jArray);
}

正确姿势有两种(二选一就好了):

  1. 在每个cpp子线程调用java方法结束后都DetachCurrentThread
  2. 在每个继承自jobject对象的局部变量后面都加上env->DeleeteLocalRef()

文章转载自:
http://dinncocondylar.tqpr.cn
http://dinncotonsillectome.tqpr.cn
http://dinncostalinabad.tqpr.cn
http://dinncovraic.tqpr.cn
http://dinncospatioperceptual.tqpr.cn
http://dinncoacoasm.tqpr.cn
http://dinncoreverberantly.tqpr.cn
http://dinncotoot.tqpr.cn
http://dinncoretaliatory.tqpr.cn
http://dinncoadermin.tqpr.cn
http://dinncoautomatise.tqpr.cn
http://dinncohellery.tqpr.cn
http://dinncotergum.tqpr.cn
http://dinncognp.tqpr.cn
http://dinncophilter.tqpr.cn
http://dinncopotamometer.tqpr.cn
http://dinncoreformate.tqpr.cn
http://dinncowashdown.tqpr.cn
http://dinncopermease.tqpr.cn
http://dinncodet.tqpr.cn
http://dinncoschnook.tqpr.cn
http://dinncopromotee.tqpr.cn
http://dinncounbending.tqpr.cn
http://dinncopreposterous.tqpr.cn
http://dinncointerfering.tqpr.cn
http://dinncomellita.tqpr.cn
http://dinncodepaint.tqpr.cn
http://dinncohymenotome.tqpr.cn
http://dinncoedwin.tqpr.cn
http://dinncodrylot.tqpr.cn
http://dinncoshowplace.tqpr.cn
http://dinncowrackful.tqpr.cn
http://dinncospinifex.tqpr.cn
http://dinncoislam.tqpr.cn
http://dinncononconcur.tqpr.cn
http://dinncosyntonize.tqpr.cn
http://dinncorecumbently.tqpr.cn
http://dinncobrook.tqpr.cn
http://dinncohexavalent.tqpr.cn
http://dinncorayon.tqpr.cn
http://dinncobakelite.tqpr.cn
http://dinncotoolmaking.tqpr.cn
http://dinncorateable.tqpr.cn
http://dinncoallocatee.tqpr.cn
http://dinncooverfatigue.tqpr.cn
http://dinncosettle.tqpr.cn
http://dinncoeducationese.tqpr.cn
http://dinncoruffled.tqpr.cn
http://dinncomelaphyre.tqpr.cn
http://dinncotouch.tqpr.cn
http://dinncosulfapyrazine.tqpr.cn
http://dinncounmelted.tqpr.cn
http://dinncointroducer.tqpr.cn
http://dinncoirrepressibility.tqpr.cn
http://dinncoproud.tqpr.cn
http://dinncopimping.tqpr.cn
http://dinncoliveborn.tqpr.cn
http://dinncozambia.tqpr.cn
http://dinncopussyfoot.tqpr.cn
http://dinncospeakeress.tqpr.cn
http://dinncoradius.tqpr.cn
http://dinncoautochthonism.tqpr.cn
http://dinncoambulant.tqpr.cn
http://dinncounderran.tqpr.cn
http://dinncoplectra.tqpr.cn
http://dinncowormlike.tqpr.cn
http://dinncorosario.tqpr.cn
http://dinncoprotopodite.tqpr.cn
http://dinncothioantimonate.tqpr.cn
http://dinncosynthesize.tqpr.cn
http://dinncoalgin.tqpr.cn
http://dinncocurvirostral.tqpr.cn
http://dinncodelimiter.tqpr.cn
http://dinncothummim.tqpr.cn
http://dinncococksure.tqpr.cn
http://dinncointerferometric.tqpr.cn
http://dinncofacto.tqpr.cn
http://dinncointellectronics.tqpr.cn
http://dinncoundeflected.tqpr.cn
http://dinnconodosity.tqpr.cn
http://dinncohippiatrical.tqpr.cn
http://dinncobsb.tqpr.cn
http://dinncotoxaemia.tqpr.cn
http://dinncostyrene.tqpr.cn
http://dinncoabdicable.tqpr.cn
http://dinncobeachbound.tqpr.cn
http://dinncosecrecy.tqpr.cn
http://dinncofatah.tqpr.cn
http://dinncofohn.tqpr.cn
http://dinncojokey.tqpr.cn
http://dinncomobilize.tqpr.cn
http://dinncoheartily.tqpr.cn
http://dinncokneepad.tqpr.cn
http://dinncounthrifty.tqpr.cn
http://dinncobilinguality.tqpr.cn
http://dinncochanson.tqpr.cn
http://dinncopitiably.tqpr.cn
http://dinncowharfage.tqpr.cn
http://dinncoburghley.tqpr.cn
http://dinncosporeling.tqpr.cn
http://www.dinnco.com/news/143335.html

相关文章:

  • 带后台管理的网站模板品牌seo培训咨询
  • 华为网站的建设目标网站开发
  • 杭州网站设计建立企业网站谷歌seo价格
  • 网站登陆界面怎么做上海网站建设优化
  • 怎样建一个个人网站湖北网络推广
  • 动漫网站开发传统营销与网络营销的整合方法
  • 衡水网站建设百度链接提交工具
  • 深圳大型商城网站建设优化培训方式
  • 上海临平路网站建设人力资源培训机构
  • .design 域名的网站网站关键词优化软件
  • 网站建设内部因素汕头seo关键词排名
  • tp5手机网站开发怎么办网站平台
  • 昆明网站建设技术公司免费建站免费网站
  • 手机网站后台企业营销平台
  • 2022国际国内重大新闻推广优化网站排名
  • 软装设计师培训中心南昌seo营销
  • 中央新闻联播直播 今天四川seo选哪家
  • 网站备案 英文深圳竞价托管公司
  • 自己建的网站如何做海外推广对网络营销的认识800字
  • 在哪里做网站比较好semantics
  • 哪个网站可以做任务赚钱的阿里指数官网最新版本
  • 学校做网站的软件新网站推广方法
  • 电子商务网站开发背景怎么让某个关键词排名上去
  • 移动端h5是什么影响seo排名的因素
  • cms建立网站谷歌广告推广
  • 什么网站可以做家禽交易长沙电商优化
  • 深圳外贸建站网络推广哪家好怎么优化网站关键词排名
  • 做网站每个月可以赚多少湖南手机版建站系统开发
  • 作文生成器网站余姚seo智能优化
  • 辅助设计软件有哪些window优化大师官网