Andorid how do we manage your memory?

Source: Internet
Author: User

Andorid how do we manage your memory?
An error occurred while working on the tank project: Java. Lang. outofmemoryerror
I didn't spend much time dealing with the memory issue at the time. At that time, I used to think that I had to think that I was just like PC programming.
I have also developed embedded systems. At that time, it was based on pure C embedded development. Program During development, you should be especially careful with pointer and memory allocation. If you are not careful, the leakage of the sub-memory will lead to a crash.
Because Java does not have pointers, and it cannot be malloc or free like in C. Java manages the allocation and release of memory by itself, so I didn't focus on the memory at the beginning, just to Java. lang. outofmemoryerror.
This type of problem is frequently encountered when processing a large number of images and developing games, so pay extra attention to it.
I have summarized and summarized the memory in the following sections. I hope you can make progress together. If there are any mistakes, please correct and improve them.
How to optimize memory management:
1. Try not to use local variables in a loop.
2. You can point unnecessary objects to null and pay attention to your own Code Quality.
3. display for GC collection
If (bitmapobject. isrecycled () = false) // if not recycled
Bitmapobject. Recycle (); // http://www.cnblogs.com/tankaixiong/
4. Scaling large images. bitmapfactory is often used when processing images. In Android, when reading bitmap, the image stack size in the virtual machine is only 8 Mb.
This error is sometimes encountered when bitmapfactory is used to decode an image. This is often caused by an excessively large image. In this case, we need to allocate less memory for storage.
Bitmapfactory. Options. insamplesize setting the appropriate insamplesize allows bitmapfactory to allocate less space to eliminate this error. For more information about insamplesize, see the SDK documentation. Android provides a dynamic computing

You can view the source code.
For example:
Bitmapfactory. Options opts = new bitmapfactory. Options ();
Opts. insamplesize = 4;
Bitmap bitmap = bitmapfactory. decodefile (imagefile, opts );
5. The Dalvik. vmruntime class provides interfaces for specific functions of the Global Virtual Machine and Dalvik.
Android can use the totalmemory () freememory () method of the runtime class to obtain some memory information of the VM. For the heap memory of the system, you can use Dalvik. vmruntime class

The getminimumheapsize () method obtains the minimum available heap memory. It also shows that releasing soft references can call the gcsoftreferences () method of this class to collect available memory.
We can also forcibly define the memory size of our software. We use the Dalvik. system. vmruntime class provided by Dalvik to set the minimum heap memory as an example:

Private Final Static int cwj_heap_size = 6*1024*1024;

Vmruntime. getruntime (). setminimumheapsize (cwj_heap_size); // set the minimum heap memory size to 6 MB. Of course, for memory compression, manual GC interference can also be performed. We will refer to the specific application next time.

The settargetheaputilization method provided by the Dalvik. system. vmruntime class can improve the processing efficiency of program heap memory. Of course, we can refer to open-source projects for specific principles,

Here we will only talk about the usage:
Private Final Static float target_heap_utilization = 0.75f;

You can call vmruntime. getruntime (). settargetheaputilization (target_heap_utilization) during oncreate.

6. cache: Use the cache in a proper amount. Do not use it excessively. Because the memory is limited, do not store image data if the path address can be saved. Do not cache images if it is not frequently used. Clear images if it is not used.

7. Try to use Android's own mechanism, because Android is based on Dalvik rather than JVM.

Inputstream is = NULL;
Try {
Is = new fileinputstream (new file (picstr ));

} Catch (filenotfoundexception e ){
// Http://www.cnblogs.com/tankaixiong/
//}
The following method is definitely better than the above method.
Resources Re = mcontext. getresources ();
Inputstream is = Re. openrawresource (picstr );

I hope you will discuss it here! Http://www.cnblogs.com/tankaixiong/

Related Article

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.