簡體   English   中英

Android bitmap用法; 雙變量 bitmap 實例是否堆積在 memory 緩存中?

[英]Android bitmap usage; does double variable bitmap instances pile up in memory cache?

When a new bitmap variable is created to have the same value as an original bitmap variable, does it build a copy thereby taking memory cache space for two bitmaps or it only returns the original bitmap cache whenever it is called within the app?

插圖是這個;

public static Bitmap originalBmp;
public static Bitmap copyBmp;

originalBmp = ......;
copyBmp = originalBmp;

我現在的問題是這個; 在 memory 緩存中,如果 originalBmp 占用 2MB,則調用copyBmp = originalBmp; 導致總緩存 memory 為 4MB,或者在調用 copyBmp 的任何地方都保持 2MB....只是要知道沒有 memory 堆滿新的 Z86BB33755628454AF74F88F047EC894 變量值

在 Java 中, originalBmpcopyBmp指向對象。 我們經常說它們“是”對象,但這是一種語言速記。

在您的情況下,當您執行copyBmp = originalBmp; , originalBmpcopyBmp都將指向同一個Bitmap object,而不是每個指向它自己的 object 副本。 因此,盡管有兩個字段指向它,但您的 2MB Bitmap仍然只消耗 2MB。

但請注意,將 2MB Bitmap放入static字段是危險的。 static字段是“故意的 memory 泄漏”。 在對Bitmap的所有引用都消失之前,無法釋放 2MB 以供其他對象使用。 In your case, both originalBmp and copyBmp are static , so they will always point to your 2MB Bitmap , until you reset both of those fields to null (or point them to some other Bitmap ). 使用static字段時要非常小心,以免最終消耗太多 memory。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM