[英]How to release memory in Renderscript in Android?
Android Studio 中的分析器顯示我的 Renderscript 應用程序在圖形 memory 中存在 memory 泄漏。 我寫了一些測試代碼並成功重現了問題,但我不知道如何解決它。
渲染腳本代碼
void memoryTest(rs_allocation output) {
rs_allocation tmp;
for(int i = 0; i < 10; i++) {
tmp = rsCreateAllocation_char(1000, 1000);
}
}
Kotlin 代碼
val rs: RenderScript = RenderScript.create(this)
while (true) {
val script = ScriptC_singlesource(rs)
val outputAllocation = Allocation.createTyped(rs, Type.createXY(rs, Element.I16(rs), 1000, 1000))
script.invoke_memoryTest(outputAllocation)
outputAllocation.destroy()
script.destroy()
}
Memory 用法 我在 15 秒時結束了運行,這就是 memory 下降的原因。
我試過free(tmp)
, delete tmp
, delete(tmp)
,都導致編譯錯誤。 我想知道如何釋放在 Renderscript 中創建的 rs_allocation 的rs_allocation
。
編輯
我嘗試rsClearObject(rs_allocation*)
,Renderscript 變成了
void memoryTest(rs_allocation output) {
rs_allocation tmp;
for(int i = 0; i < 10; i++) {
rsClearObject(&tmp);
tmp = rsCreateAllocation_char(1000, 1000);
}
}
它沒有解決問題。 memory 用法保持不變。 它似乎沒有任何效果。
終於解決了。 您只需要在循環內使用另一個變量來創建rs_allocation
,然后將其傳遞給外部變量。
void memoryTest(rs_allocation output) {
rs_allocation tmp;
for(int i = 0; i < 10; i++) {
rs_allocation tmp2 = rsCreateAllocation_char(1000, 1000);
tmp = tmp2;
//In my case tmp is a global variable used to pass value to kernel functions,
//so it has to be outside.
somefunctionThatUseTmp();
}
}
我無法解釋為什么會這樣,它違背了我對指針在 C 中如何工作的理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.