Android中開啟一個空線程會佔用多少記憶體

來源:互聯網
上載者:User

Android開發中,開啟一個線程會佔用多少記憶體空間?這個問題我一直沒有測試過,以前在網上看見別人說需要1M記憶體(可能是該網友包含了很多資料),今天對這個問題做了一個測試。為了不影響測試,我使用空線程(線程不做任何事情,也不包含任何資料)。

先貼上測試代碼

thread_occupy_memory.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button android:id="@+id/start_thread"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="開啟一個新線程" /><TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textSize="20dp"        android:text="記憶體" /><TextView android:id="@+id/show_memoty"        android:layout_width="fill_parent"        android:layout_height="fill_parent" /></LinearLayout>

import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;/** * 測試開啟一個空線程會佔用多少記憶體 * @author DaveeChen */public class ThreadOccupyMemoryActivity extends Activity {private TextView mTextView;private ArrayList<Object> threadList = new ArrayList<Object>();@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.thread_occupy_memory);init();}private void init() {this.findViewById(R.id.start_thread).setOnClickListener(new OnClickListener() {public void onClick(View v) {MyThread mMyThread = new MyThread();threadList.add(mMyThread);mMyThread.start();}});mTextView = (TextView)this.findViewById(R.id.show_memoty);mTextView.setText(analyzeMemory());}private String analyzeMemory() {Runtime mRuntime = Runtime.getRuntime();long usedMemory = mRuntime.totalMemory() - mRuntime.freeMemory();String result = "線程數量:"+threadList.size()+"\nUsedMemory:"+usedMemory+"bytes;\n"+"---------------------------------\n";return result;}private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (msg.what == 1) {mTextView.setText(msg.obj.toString() + mTextView.getText());}}};private class MyThread extends Thread {@Overridepublic void run() {mHandler.sendMessage(mHandler.obtainMessage(1, analyzeMemory()));while(true) {//讓線程保持一直運行}}}}

  

在啟動線程之前,已使用記憶體3015936位元組(大約3015K)。啟動一個線程後,記憶體已使用3030904(大約3030K),說明開啟第一個線程使用了大約15K記憶體;

當開啟了10個線程之後,記憶體已使用3071064(大約3071K),說明開啟10個空線程大約用了55K記憶體。

我也測試了AsyncTask,佔用記憶體和Thread差不多

private class MyAsyncTask extends AsyncTask<Void,Void,Void> {@Overrideprotected Void doInBackground(Void... params) {mHandler.sendMessage(mHandler.obtainMessage(1, analyzeMemory()));while(true) {//讓線程保持一直運行}//return null;}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.