2

I want develop android application and in this application use many methods. but sometime when work with application show me this error in LogCat :

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ir.mototel.mototel, PID: 4899
                  java.lang.OutOfMemoryError: Failed to allocate a 490012 byte allocation with 316664 free bytes and 309KB until OOM
                      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
                      at android.content.res.Resources.loadDrawable(Resources.java:2540)
                      at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                      at android.widget.ImageView.<init>(ImageView.java:152)
                      at android.widget.ImageView.<init>(ImageView.java:140)
                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
                      at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                      at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1017)
                      at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1076)
                      at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                      at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                      at com.sepandar.xengine.singleVideo.view.DetailFragment.onCreateView(DetailFragment.java:182)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                      at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
                      at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
                      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
                      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
                      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:770)
                      at android.view.View.measure(View.java:18788)
                    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.

i Write this code in Application tag in Manifest android:largeHeap="true" but show me this error again!

How can i fix it? Please help me <3

Dj.Soa
  • 23
  • 1
  • 3
  • Try using resources of a lesser size – Mushirih Dec 04 '16 at 07:51
  • issue with loading Images with larger size, may be you can try with other image with some small size and better for performance. You may solve the error but low memory always effect other thread performance may add log in User Interface. – Swapnil Dec 04 '16 at 07:51
  • Are you showing a large number of images? – Mithun Sarker Shuvro Dec 04 '16 at 08:00
  • @shuvro, yes my friend – Dj.Soa Dec 04 '16 at 08:02
  • @Dj.Soa are you fetching images from web via some api or just the local images you have put in your drawable folder? – Mithun Sarker Shuvro Dec 04 '16 at 08:04
  • @shuvro, no i fetching images from server and show in my application. how can i fix it? please help me – Dj.Soa Dec 04 '16 at 08:12
  • @Dj.Soa please share your code for showing images – Mithun Sarker Shuvro Dec 04 '16 at 08:14
  • @shuvro, my application is big and this code is large. where code you want? – Dj.Soa Dec 04 '16 at 08:16
  • @Dj.Soa code for your imageView and displaying images – Mithun Sarker Shuvro Dec 04 '16 at 08:18
  • How do you think we could help with such a problem? You have a huge complex application, and that application is either simply using too much memory, or it contains one (or several) bugs that lead to out-of-memory situations. There is **nothing** that people without access to your source code could do for you. It is your app; so **you** have to learn how to do memory profiling on Android to understand what **your** app is actually doing. And then, when you have a specific question, you are welcome to come back and ask about that one. But as of now, all the relevant information is on your end! – GhostCat Dec 04 '16 at 08:19
  • @shuvro, i write this code in **ViewHolder** class : `Glide.with(context).load(content.getThumbImage()).asBitmap().transform(new CenterCrop(context)).placeholder(R.drawable.placeholder).into(thumbnail);` – Dj.Soa Dec 04 '16 at 08:21
  • @shuvro, can you help me my friend? – Dj.Soa Dec 04 '16 at 08:26
  • @Dj.Soa check my answer and let me if it works – Mithun Sarker Shuvro Dec 04 '16 at 08:34
  • Possible duplicate of [OutOfMemoryError](http://stackoverflow.com/questions/2021643/outofmemoryerror) – Yogesh Rathi Dec 04 '16 at 12:08

1 Answers1

0

Create a folder named drawable-nodpi inside your res folder . Put all of your images you wanted to show including the placeholder images into that folder. After that Load image like this

Glide.with(context)
   .load(content.getThumbImage())
   .asBitmap()‌​.transform(new CenterCrop(context))
   .diskCacheStrategy( DiskCacheStrategy.NONE )
   .skipMemoryCache( true )
   .placeholder(R.drawable.placeholder)
   .int‌​o(thumbnail)

Let me know if this works for you

Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64
  • I not want save this images into phone **(drawable-nodpi)**, i want just load from server and show in `imageview`. not save images. if i just add this codes `.diskCacheStrategy( DiskCacheStrategy.NONE ) .skipMemoryCache( true )` fix my problem? what is work this codes? – Dj.Soa Dec 04 '16 at 08:37
  • @Dj.Soa I asked you in the comment that "are you fetching images from web via some api "? You said no . By the way , then just put your placeholder images into drawable-nodpi , I hope it will fix your problem – Mithun Sarker Shuvro Dec 04 '16 at 08:40
  • @Shuvaro, sorry my friend sorry. i just should this images from server and not save into phone. is work for this issue you above code? – Dj.Soa Dec 04 '16 at 08:43
  • Thank you my friend, and final question. please ask this <3 . why i should add this codes `.diskCacheStrategy( DiskCacheStrategy.NONE ) .skipMemoryCache( true )` ? when added this codes, not problem for my request from server? Thanks – Dj.Soa Dec 04 '16 at 08:47
  • As you are loading a huge number of images and it cached into device memory when the memory limit exceeds you get this error but if you add this, images aren't simply cached on your device. Hence there is no out of memory error. If it solves your problem please accept this answer . – Mithun Sarker Shuvro Dec 04 '16 at 08:51