Recently wrote a test demo debugging network optimization, found downloading 20M files when I directly applied for 20M space, and then oom led to crash ~ ~
The typical error message is as follows:
Outofmemoryerror:out of Memory:heap size=33887kb, allocated=22009kb, limit=49152kb
1, Baidu, some people say that the resources did not release the memory leak caused, this situation can only optimize the memory management, I this obviously is not, I just want to use so much!!
2, see a little brother did the processing can load 5m*20 picture a little thing not, pure code sharing (shoot brick expression in this!!) link at times)
3, this little brother is more domineering (link here), the proposed method:
- The
- modifies the memory size limit definition (however: the function setminimumheapsize actually only changes the lower limit of the heap, which prevents too frequent heap memory allocations, and still uses the heap's upper limit when setting the minimum heap memory size above the upper limit value (Max heap size). Doesn't work for low memory.
private final static int cwj_heap_size = 6* 1024* 1024; Vmruntime.getruntime (). Setminimumheapsize (Cwj_heap_size); // Set the minimum heap memory to 6MB size. private final static int cwj_heap_size = 6* 1024* 1024;
Vmruntime.getruntime (). Setminimumheapsize (Cwj_heap_size); //
- Manual interference GC
Private Final static floattarget_heap_utilization = 0.75f; Private Final static floattarget_heap_utilization = 0.75f; // OnCreate vmruntime.getruntime (). Settargetheaputilization (target_heap_utilization);
- Ndk
4, this small brother summed up very well (link here)
- Cache images into memory, using soft references to cache to memory, instead of reloading to memory every time it is used;
- Adjust the image size, mobile phone screen size is limited, the display area assigned to the image itself is smaller, sometimes the image size can be adjusted appropriately;
- Using low memory consumption of encoding, such as Bitmap.Config.ARGB_4444 than Bitmap.Config.ARGB_8888 more memory;
- Timely recovery of images, if the reference to a large number of bitmap objects, and the application does not need to display all the pictures at the same time, can be temporarily used bitmap objects in time to reclaim;
- Custom heap memory allocation size optimizes heap memory allocation for Dalvik virtual machines;
5, this little brother said more clearly some (link here)
Summarize:
In fact, my problem here is mainly entityutils.tostring when the output is too large.
Refer to Https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/JsonStreamParser.html, in general, Can't load a lot of data into memory at once!!
In fact, you can refer to the entityutils.tostring of their own way to achieve a not all of a sudden to the string in the method:
1 Public LongReadresponse (httpentity entity)throwsioexception{2 LongResponseLength = 0;3 4 inti = (int) entity.getcontentlength ();5 if(I < 0) {6i = 4096;7 }8 9InputStream instream =entity.getcontent ();TenInputStreamReader reader =NewInputStreamReader (instream); One //Chararraybuffer buffer = new Chararraybuffer (i); A - Try { - Char[] tmp =New Char[1024]; the - intl; - while((L = reader.read (tmp))! =-1) { - //buffer.append (tmp, 0, L); +ResponseLength + =tmp.length; - } +}finally { A reader.close (); at } - returnResponseLength; -}
Understanding of Android OutOfMemoryError