Android Simple Combat tutorial--the 12th session of the code to get the total running memory size of the phone

Source: Internet
Author: User
<span id="Label3"></p><p><p>Cell phone Ram storage, similar to the memory of the Computer. This article is a detailed description of the total memory size of the phone through the Code.</p></p><p><p>first, define an engine class, which is the ability to get process information, including the number of programs running, total system memory, and total system memory. This article completes first to obtain the total memory.</p></p><p><p>The code is as Follows:</p></p><pre code_snippet_id="1804635" snippet_file_name="blog_20160803_1_8867539" name="code" class="html"><pre code_snippet_id="1804635" snippet_file_name="blog_20160803_1_8867539" name="code" class="html">public class Taskmanagerengine {//get total memory size public static long gettotalmemorysize (context context) {long size = 0;// Get Activitymanager management, to get "run-related" information about Activitymanager Activitymanager = (activitymanager) related to running information Context.getsystemservice (context. activity_service); Memoryinfo outinfo = new Memoryinfo ();//outinfo object contains memory-related information Activitymanager.getmemoryinfo (outinfo);// Pass memory-related information to outinfo inside C + + thought size = outinfo.totalmem;//gets The total internal size by reading the configuration File. File Directory:/proc/meminforeturn size;}}</pre></pre>In fact, if it is to get the remaining memory, change the Size=outinfo.totalmem to Size=outinfo.avalimem. and will not Error. however, the above program will error, because only support api16 level above the PHONE. Obviously this is not feasible for low-version Users. So long to find another way to get the total memory size of the Phone-file Acquisition. The file is the proc directory<br><p><p>For file acquisition, first through the Inux instruction, see that the file has memory details</p></p><p><p>The Linux directives are as Follows:</p></p><p><p><span style="color:#008000">Cd/proc</span><br><span style="color:#008000">LS enters the directory</span><br><span style="color:#008000">Ls-l View more information</span><br><span style="color:#008000">Cat Cupinfo View detailed information in Cpuinfo</span><br><span style="color:#008000">Cat Meminfo View Meminfo memory details</span><br><span style="color:#008000"><br></span></p></p><p><p><span style="color:#008000">Detailed information is as Follows:</span></p></p><p><p></p></p><p><p>The first row position is the total memory size in the System. Depending on the file information, the total memory size can be Obtained. Here's How:</p></p><p><p></p></p><pre code_snippet_id="1804635" snippet_file_name="blog_20160803_2_6501771" name="code" class="html"><pre code_snippet_id="1804635" snippet_file_name="blog_20160803_2_6501771" name="code" class="html">public class Taskmanagerengine {//get total memory size public static long gettotalmemorysize (context context) {long size = 0;// Gets the total internal size by reading the configuration File. Files Directory:/proc/meminfofile file = new file ("/proc/meminfo"), try {bufferedreader reader = new BufferedReader (new InputStreamReader (file));//according to the command line, the total system memory size is located in the first row of string totalmemarysizestr = Reader.readline ( );//memtotal: 513744 KB //to get the size, intercept the string int startIndex = totalmemarysizestr.indexof (': '); int endIndex = Totalmemarysizestr.indexof (' k ');//intercept totalmemarysizestr = totalmemarysizestr.substring (startIndex + 1, endIndex). Trim ();//to A Long type, the data unit is kbsize = Long.parselong (totalmemarysizestr);//to byte size *= 1024;} Catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Return size;}}</pre></pre><br>The size returned is the size of the phone running memory. Unit tests get Data: 526073856<br><p><p></p></p><p><p><br></p></p><p><p>Android Simple Combat tutorial--the 12th session of the code to get the total running memory size of the phone</p></p></span>
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.