2016年3月11日Android實習日記

1.明天刪除orthodotics_design_animation_content_gif.gif文件。(已完成)html

2. 如何檢測內存泄露? java

A: 能夠經過一些性能監測分析工具,如 JProfiler、Optimizeit Profiler,MAT工具。android

 MAT工具參考http://blog.csdn.net/fenglibing/article/details/6298326數組

Q: 如何避免內存泄露、溢出?
A: 1) 儘早釋放無用對象的引用。安全

好的辦法是使用臨時變量的時候,讓引用變量在退出活動域後自動設置爲null,暗示垃圾收集器來收集該對象,防止發生內存泄露。網絡

2) 程序進行字符串處理時,儘可能避免使用String,而應使用StringBuffer。jsp

由於每個String對象都會獨立佔用內存一塊區域,如: 工具

Java代碼性能

String str = "aaa";
String str2 = "bbb";
String str3 = str str2;
// 假如執行這次以後str , str2再不被調用,那麼它們就會在內存中等待GC回收;
// 假如程序中存在過多的相似狀況就會出現內存錯誤;
String str = "aaa";String str2 = "bbb"; String str3 = str str2; // 假如執行這次以後str , str2再不被調用,那麼它們就會在內存中等待GC回收; // 假如程序中存在過多的相似狀況就會出現內存錯誤;大數據

 

3) 儘可能少用靜態變量。

由於靜態變量是全局的,GC不會回收。

4) 避免集中建立對象尤爲是大對象,若是能夠的話儘可能使用流操做。

JVM會忽然須要大量內存,這時會觸發GC優化系統內存環境;

JVM會忽然須要大量內存,這時會觸發GC優化系統內存環境; 一個案例以下:

      Java代碼:

// 使用jspsmartUpload做文件上傳,運行過程當中常常出現java.outofMemoryError的錯誤,
// 檢查以後發現問題:組件裏的代碼
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];
// totalBytes這個變量獲得的數極大,致使該數組分配了不少內存空間,並且該數組不能及時釋放。
// 解決辦法只能換一種更合適的辦法,至少是不會引起outofMemoryError的方式解決。
// 參考:http://bbs.xml.org.cn/blog/more.asp?name=hongrui&id=3747
// 使用jspsmartUpload做文件上傳,運行過程當中常常出現java.outofMemoryError的錯誤, // 檢查以後發現問題:組件裏的代碼 m_totalBytes = m_request.getContentLength(); m_binArray = newbyte[m_totalBytes]; // totalBytes這個變量獲得的數極大,致使該數組分配了不少內存空間,並且該數組不能及時釋放。 // 解決辦法只能換一種更合適的辦法,至少是不會引起outofMemoryError的方式解決。 // 參考:http://bbs.xml.org.cn/blog/more.asp?name=hongrui&id=3747

  

5) 儘可能運用對象池技術以提升系統性能。

生命週期長的對象擁有生命週期短的對象時容易引起內存泄漏,例如大集合對象擁有大數據量的業務對象的時候,能夠考慮分塊進行處理,而後解決一塊釋放一塊的策略。

6) 不要在常常調用的方法中建立對象,尤爲是忌諱在循環中建立對象。

能夠適當的使用hashtable,vector 建立一組對象容器,而後從容器中去取那些對象,而不用每次new以後又丟棄。

7) 優化配置。

參考:http://blog.csdn.net/wisgood/article/details/16818243

另外參考:http://blog.csdn.net/seelye/article/details/8269705

3.android Handler應設爲static,android開發中,使用Lint檢測時會提示這麼一句話 : This Handler class should be static or leaks might occur。參考:http://blog.csdn.net/jdsjlzx/article/details/8463428

4. linearlayout中如何讓某個控件,位於整個屏幕正中央。參考:http://www.eoeandroid.com/thread-321860-1-1.html?_dsign=4c07eb92

個人程序代碼:

<!-- 網絡鏈接提示信息 -->
    <LinearLayout
        android:id="@+id/before_connect_server"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginBottom="50dp"
        android:gravity="center">

                <ProgressBar
                    style="@android:style/Widget.ProgressBar.Large"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="正在加載試題,請稍後..."
                    android:textSize="17sp"
                    android:gravity="center"
                    android:layout_marginTop="20dp"
                    android:textColor="#666666"/>


    </LinearLayout>

  

5.

//加載好試題以前,先隱藏內容界面
mSv.setVisibility(View.GONE);而不是
mSv.setVisibility(View.INVISIBLE);

6.

線程中加入showToast("未鏈接網絡", 200);出錯,直接跳出。
//mServerConnectStateHandler.sendEmptyMessage(MsgWhat.CONNECT_SERVER_FAIL);
//Toast.makeText(getApplicationContext(), "鏈接失敗", 200).show();
緣由:線程中不能更新UI相關元素,應使用handler。
anroid 線程不安全 須要用handler 否則你run裏面的貌似不能執行。

7.

相關文章
相關標籤/搜索