Android memory overflow

Source: Internet
Author: User

 

Although Android automatically manages the memory, Java also has the garbage collection (GC) memory recycle mechanism.

However, if the program opens several M files in one operation, the following error message is usually displayed.

02-04 21:46:08. 703: Error/dalvikvm-heap (2429): 1920000-byte external allocation too large for this process.

Or
02-04 21:52:28. 463: Error/androidruntime (2429): Java. Lang. outofmemoryerror: bitmap size exceeds VM budget

Mobile Terminals often encounter the above errors due to limited memory.

Solution:

1. explicitly call system. GC ();

This kind of memory reclaim will have a certain effect, but please do not look forward to it too much.

2. Reclaim memory after image processing is complete.

Please call bitmap for image processing and then reclaim the memory.

Bitmap. Recycle ();

In this way, the memory occupied by the previously used images will be released.

3. Specify the image size during image processing.

The following method is required when processing several m images.

View plaincopy to clipboardprint?
Bitmap getbitpmap (){
Parcelfiledescriptor PFD;
Try {
PFD = mcon. getcontentresolver (). openfiledescriptor (Uri, "R ");
} Catch (ioexception ex ){
Return NULL;
}
Java. Io. filedescriptor FD = PFD. getfiledescriptor ();
Bitmapfactory. Options = new bitmapfactory. Options ();
// Specify the original size first
Options. insamplesize = 1;
// Only determine the size
Options. injustdecodebounds = true;
// Call this method to obtain the size of the image in options.
Bitmapfactory. decodefiledescriptor (FD, null, options );
// Our goal is to display the image at 800pixel.
// Therefore, you need to call computesamplesize to obtain the image scaling ratio.
Options. insamplesize = computesamplesize (options, 800 );
// OK. We get the scaling ratio. Now we start to officially read bitmap data.
Options. injustdecodebounds = false;
Options. indither = false;
Options. inpreferredconfig = bitmap. config. argb_8888;

// Reduce the memory according to the options Parameter
Bitmap sourcebitmap = bitmapfactory. decodefiledescriptor (FD, null, options );

Return sourcebitmap;
}
// This function determines the image size and obtains the appropriate scaling ratio, for example, 2: 1/2, 3: 1/3.
Static int computesamplesize (bitmapfactory. Options options, int target ){
Int W = options. outwidth;
Int H = options. outheight;
Int candidatew = W/target;
Int candidateh = H/target;
Int candidate = math. Max (candidatew, candidateh );
If (Candidate = 0)
Return 1;
If (Candidate> 1 ){
If (W> Target) & (W/candidate) <target)
Candidate-= 1;
}
If (Candidate> 1 ){
If (h> Target) & (H/candidate) <target)
Candidate-= 1;
}

 

The above solution is used, but this is not the perfect solution.

For more information, see:

Optimize heap memory allocation of Dalvik virtual machines

For the Android platform, the Dalvik Java VM used by its hosting layer can be optimized from the current performance, for example, when developing large game or resource-consuming applications, we may consider Manual Interference with GC and use Dalvik. system. the settargetheaputilization method provided by the vmruntime class can improve the processing efficiency of the program heap memory. Of course, we can refer to open-source projects for specific principles. Here we only talk about the usage: Private Final Static float target_heap_utilization = 0.75f; it can be called during oncreate.
Vmruntime. getruntime (). settargetheaputilization (target_heap_utilization.

You can also customize the size of the android heap memory.

For some Android projects, the main cause of performance bottleneck is the memory management mechanism of Android. Currently, mobile phone manufacturers are stingy with Ram and are very sensitive to the impact of RAM on the Performance of software fluency, in addition to optimizing the heap memory allocation of the Dalvik virtual machine, we can also forcibly define the memory size of our software. We use the Dalvik provided by Dalvik. system. the following example uses the vmruntime class to set the minimum heap memory:

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.
If (verbose)
Log. V (TAG, "for W/H" + W + "/" + H + "returning" + candidate + "(" + (W/candidate) + "/" + (H/candidate ));

Return candidate;
}

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.