Android get memory information (RAM,ROM)

Posted by glcarlstrom on Sat, 04 Jan 2020 07:21:32 +0100

1, Brief introduction to RAM and ROM

RAM refers to running memory. For example, the floating window of 360 mobile assistant often prompts that the running memory is more than 80%. It refers to running memory. The general size is a few G.

ROM is the memory for storing data. For example, the "total space 31.6G, the remaining 28.8G" displayed by iqiyi APP on the video page refers to ROM. It's usually dozens of G, hundreds of G. The larger the ROM, the more videos, files, music, etc. can be stored.

 

2, How to get ram and ROM from Android

1. Layout: very simple, just two textviews

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    
    
    

</LinearLayout>

2. Corresponding methods and descriptions in Java code:

public class MainActivity extends Activity{
	private TextView tv1;
	private TextView tv2;


	@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_memory_test);
		tv1 = (TextView) findViewById(R.id.textView1);
		tv2 = (TextView) findViewById(R.id.textView2);
		
		//Get run memory information
		ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);  
        MemoryInfo info = new MemoryInfo();  
        manager.getMemoryInfo(info);  
        StringBuilder sb = new StringBuilder();
        sb.append("available RAM:");
        sb.append(info.availMem + "B");
        sb.append(",total RAM:");
        sb.append(info.totalMem + "B");
        sb.append("\r\n");
        sb.append(Formatter.formatFileSize(getBaseContext(), info.availMem));
        sb.append(",");
        LogUtil.print("totalMem:" + info.totalMem);
        sb.append(Formatter.formatFileSize(getBaseContext(), info.totalMem));
        tv1.setText(sb);
        
        
        
        sb.setLength(0);
        //Get ROM memory information
        //Call this class to get disk information (getDataDirectory is the internal store)
        final StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
        long totalCounts = statFs.getBlockCountLong();//Total number of block s
        long availableCounts = statFs.getAvailableBlocksLong() ; //Get the number of block s available
        long size = statFs.getBlockSizeLong(); //The size of each cell, generally 4KB==
        long availROMSize = availableCounts * size;//Available internal storage size
        long totalROMSize = totalCounts *size; //Total internal storage size
        sb.append("available block number:" + availableCounts);
        sb.append("block Total:" + totalCounts);
        sb.append("\r\n");
        sb.append(" each block Size:" + size);
        sb.append("\r\n");
        sb.append(" available ROM:" + availROMSize + "B");
        sb.append(" total ROM:" + totalROMSize + "B");
        tv2.setText(sb);
        
        
	}
	
	
	
	
}

3. Effect display

 

Topics: Android Mobile xml encoding