Optimized android memory for image reading

Source: Internet
Author: User

Public class Main extends Activity {int number = 1000; Drawable [] array; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); array = new BitmapDrawable [number]; for (int I = 0; I </SPAN> number; I ++) {Log. e ("", "test No." + (I + 1) + "image"); array [I] = getResources (). getDrawable (R. drawable. img) ;}} output result: 04-07 21:49:25. 248: D/ Szipinf (7828): Initializing inflate state 04-07 21:49:25. 398: E/(7828): test 1st images 04-07 21:49:25. 658: D/dalvikvm (7828): GC_EXTERNAL_ALLOC freed 48 K, 50% free 2692 K/5379 K, external 0 K/0 K, paused 24 ms 04-07 21:49:25. 748: E/(7828): Test 2nd images 04-07 21:49:25. 748: E/(7828): Test 3rd images .................. .................. 21:49:26 04-07. 089: E/(7828): Test 998th images 04-07 21:49:26. 089: E/(7828): Test 999th images 04-07 21:49:26. 089: E/(7828): no error is reported in the test of the 1,000th images program. It runs normally. It is okay to load 1000 Drawable objects. Next let's take a look at the code for loading 1000 Bitmap objects. Similarly, the code is very simple and I will not explain it! Public class Main extends Activity {int number = 1000; Bitmap bitmap []; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); bitmap = new Bitmap [number]; for (int I = 0; I </SPAN> number; I ++) {Log. e ("", "test No." + (I + 1) + "image"); bitmap [I] = BitmapFactory. decodeResource (getResources (), R. drawable. img) ;}} output result: 04-07 22: 06:05. 344: D/szipinf (7937): Initializing inflate state 04-07 22:06:05. 374: E/(7937): test 1st images 04-07 22:06:05. 544: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed 51 K, 50% free 2692 K/5379 K, external 0 K/0 K, paused 40 ms 04-07 22:06:05. 664: E/(7937): Test 2nd images 04-07 22:06:05. 774: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed 1 K, 50% free 2691 K/5379 K, external 6026 K/7525 K, paused 31 ms 04-07 22:06:05. 834: E/(7937 ): Test 3rd images: 04-07 22:06:05. 934: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed <1 K, 50% free 2691 K/5379 K, external 12052 K/14100 K, paused 24 ms 04-07 22:06:06. 004: E/(7937): Test 4th images 04-07 22:06:06. 124: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed <1 K, 50% free 2691 K/5379 K, external 18078 K/20126 K, paused 27 ms 04-07 22:06:06. 204: E/(7937): Test 5th images 04-07 22:06:06. 315: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed <1 K, 50% free 2691 K/5379 K, external 24104 K/26152 K, paused 26 ms 04-07 22:06:06. 395: E/(7937): Test 6th images 04-07 22:06:06. 495: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed <1 K, 50% free 2691 K/5379 K, external 30130 K/32178 K, paused 22 ms 04-07 22:06:06. 565: E/(7937): Test 7th images 04-07 22:06:06. 665: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed <1 K, 50% free 2691 K/5379 K, external 36156 K/38204 K, paused 22 ms 04-07 22:06:06. 745: E/(7937): Test 8th images 04-07 22:06:06. 845: D/dalvikvm (7937): GC_EXTERNAL_ALLOC freed 2 K, 51% free 2689 K/5379 K, external 42182 K/44230 K, paused 23 ms 04-07 22:06:06. 845: E/dalvikvm-heap (7937): 6170724-byte external allocation too large for this process. 22:06:06 04-07. 885: I/dalvikvm-heap (7937): Clamp target GC heap from 48.239 MB to 48.000 MB 04-07 22:06:06. 885: E/GraphicsJNI (7937): VM won't let us al Locate 6170724 bytes 04-07 22:06:06. 885: D/dalvikvm (7937): GC_FOR_MALLOC freed <1 K, 51% free 2689 K/5379 K, external 42182 K/44230 K, paused 25 ms 04-07 22:06:06. 885: D/AndroidRuntime (7937): Shutting down VM 04-07 22:06:06. 885: W/dalvikvm (7937): threadid = 1: thread exiting with uncaught exception (group = 0x40015560) 04-07 22:06:06. 885: E/AndroidRuntime (7937): fatal exception: main 04-07 22:06:06. 885: E/ NdroidRuntime (7937): java. lang. outOfMemoryError: bitmap size exceeds VM budget 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. graphics. bitmap. nativeCreate (Native Method) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. graphics. bitmap. createBitmap (Bitmap. java: 477) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. graphics. bitmap. createBitmap (Bitmap. java: 444) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. graphics. bitmap. createScaledBitmap (Bitmap. java: 349) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. graphics. bitmapFactory. finishDecode (BitmapFactory. java: 498) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. graphics. bitmapFactory. decodeStream (BitmapFactory. java: 473) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. graphics. bitmapFactory. decod EResourceStream (BitmapFactory. java: 336) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. graphics. bitmapFactory. decodeResource (BitmapFactory. java: 359) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. graphics. bitmapFactory. decodeResource (BitmapFactory. java: 385) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at bassy. test. drawable. main. onCreate (Main. java: 37) 22:06:06 04-07. 885: E/AndroidRu Ntime (7937): at android. app. instrumentation. callActivityOnCreate (Instrumentation. java: 1047) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. app. activityThread. extends mlaunchactivity (ActivityThread. java: 1722) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. app. activityThread. handleLaunchActivity (ActivityThread. java: 1784) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. app. activi TyThread. access $1500 (ActivityThread. java: 123) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. app. activityThread $ H. handleMessage (ActivityThread. java: 939) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. OS. handler. dispatchMessage (Handler. java: 99) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at android. OS. logoff. loop (logoff. java: 130) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at androi D. app. activityThread. main (ActivityThread. java: 3835) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at java. lang. reflect. method. invokeNative (Native Method) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at java. lang. reflect. method. invoke (Method. java: 507) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at com. android. internal. OS. zygoteInit $ MethodAndArgsCaller. run (ZygoteInit. java: 847) 04-07 22:06:06. 885: E/And RoidRuntime (7937): at com. android. internal. OS. zygoteInit. main (ZygoteInit. java: 605) 04-07 22:06:06. 885: E/AndroidRuntime (7937): at dalvik. system. nativeStart. the main (Native Method) takes a look at the above output and loads 8th images. Then, the program reports an error "java. lang. outOfMemoryError: bitmap size exceeds VM budget ". The preceding example shows that using Drawable to save image objects occupies less memory. The use of Biamtp objects will occupy a lot of memory space and it is easy to see OOM! Next let's take a look at an example. This is also the loading of Bitmap objects. However, instead of using the decodeResource method of BitmapFactory, decodeStream is used to view the code. Public class Main extends Activity {int number = 1000; Bitmap bitmap []; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); bitmap = new Bitmap [number]; for (int I = 0; I </SPAN> number; I ++) {Log. e ("", "test No." + (I + 1) + "image"); bitmap [I] = BitmapFactory. decodeStream (getResources (). openRawResource (R. drawable. img); // The method is changed here }}} Output result: 04-07 22:16:12. 676: E/(8091): Test 561st images 04-07 22:16:12. 756: E/(8091): Test 562nd images 04-07 22:16:12. 826: E/(8091): Test 563rd images 04-07 22:16:12. 906: E/(8091): Test 564th images 04-07 22:16:12. 906: D/skia (8091): ---------- mmap failed for imageref_ashmem size = 2744320 err = 12 04-07 22:16:12. 906: E/(8091): Test 565th images 04-07 22:16:12. 906: D/skia (8091): ---------- mmap failed for imageref_ashmem size = 2744320 Err = 12 22:16:12-07. 906: E/(8091): Test 566th images 04-07 22:16:12. 916: E/filemap (8091): mmap (416798) failed: Out of memory 04-07 22:16:12. 916: D/filemap (8091): munmap (0x0, 0) failed 04-07 22:16:12. 916: W/asset (8091): create map from entry failed 04-07 22:16:12. 916: D/AndroidRuntime (8091): Shutting down VM 04-07 22:16:12. 916: W/dalvikvm (8091): threadid = 1: thread exiting with uncaught exception (group = 0x40015560) 04-07 22:16:12. 936: E/AndroidRuntime (8091): fatal exception: main 04-07 22:16:12. 936: E/AndroidRuntime (8091): java. lang. runtimeException: Unable to start activity ComponentInfo {bassy. test. drawable/bassy. test. drawable. main}: android. content. res. resources $ NotFoundException: File res/drawable-mdpi/img.png from drawable resource ID #0x7f020001 04-07 22:16:12. 936: E/AndroidRuntime (8091): T android. app. activityThread. extends mlaunchactivity (ActivityThread. java: 1768) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. app. activityThread. handleLaunchActivity (ActivityThread. java: 1784) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. app. activityThread. access $1500 (ActivityThread. java: 123) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. app. activityThread $ H. handleMessage (CtivityThread. java: 939) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. OS. handler. dispatchMessage (Handler. java: 99) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. OS. logoff. loop (logoff. java: 130) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. app. activityThread. main (ActivityThread. java: 3835) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at java. lang. reflect. method. invokeNative (Native Method) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at java. lang. reflect. method. invoke (Method. java: 507) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at com. android. internal. OS. zygoteInit $ MethodAndArgsCaller. run (ZygoteInit. java: 847) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at com. android. internal. OS. zygoteInit. main (ZygoteInit. java: 605) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at dalvik. System. nativeStart. main (Native Method) 04-07 22:16:12. 936: E/AndroidRuntime (8091): Caused by: android. content. res. resources $ NotFoundException: File res/drawable-mdpi/img.png from drawable resource ID #0x7f020001 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. content. res. resources. openRawResource (Resources. java: 860) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. content. res. resource S. openRawResource (Resources. java: 836) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at bassy. test. drawable. main. onCreate (Main. java: 43) 22:16:12 04-07. 936: E/AndroidRuntime (8091): at android. app. instrumentation. callActivityOnCreate (Instrumentation. java: 1047) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. app. activityThread. extends mlaunchactivity (ActivityThread. java: 1722) 04-07 22:16:12. 936: E/ AndroidRuntime (8091 ):... 11 more 04-07 22:16:12. 936: E/AndroidRuntime (8091): Caused by: java. io. fileNotFoundException: res/drawable-mdpi/img.png 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. content. res. assetManager. openNonAssetNative (Native Method) 04-07 22:16:12. 936: E/AndroidRuntime (8091): at android. content. res. assetManager. openNonAsset (AssetManager. java: 429) 04-07 22:16:12. 936: E/ DroidRuntime (8091): at android. content. res. resources. openRawResource (Resources. java: 857) 04-07 22:16:12. 936: E/AndroidRuntime (8091 ):... 15 more from the above we can see that the OOM error occurs when the program loads 566th sheets. However, compared with the 2nd examples, you will find that the program can load more images. This indicates that the decodeResource method using BitmapFactory occupies a large amount of memory, while the decodeStream method occupies a smaller memory. In terms of time, looking at the log output, we probably estimated the time required to load an image. We found that it takes about 0.17 seconds for decodeResource to load the image. Instead, we use the decodeStream method, you only need to offer an offer of 0.08 seconds! This shows that decodeStream is superior to decodeResource in terms of time and space !! From the above three examples, we can see that using the first method (using Drawable to load images) can load more images, and loading 32 images takes about 0.01 seconds! I tried to adjust the number of Drawable to 1000000. When the program is running, it stops in the 153761 images. The mobile phone prompts, "the application has no response ..." I guess that Drawable should not belong to the resident memory object. Otherwise, it is impossible to avoid OOM ~~ There is too little information about Drawable and Bitmap on the Internet, so you cannot study it in depth ~ I did another test just now, and set array [I] = getResources () in the first example (). getDrawable (R. drawable. img); the method is replaced by array [I] = Drawable. createFromStream (getResources (). openRawResource (R. drawable. img), null); the result is the same as that in the third example. An OOM error occurs in the 566th images!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.