7. TITLE :+ 연산자
System.out.println(“y의 값은: ” + y);
Log.e(“”, A+B+C+D+E+F+G+H+I+J+K+L+M+N);
+ 하는 String 의 갯수가 늘어나면 늘어날수록
GC 대상 object 수도 함께 늘어나고 그만큼 memory 할당도 추가로 된다.
REASON
8. TITLE : DB compileStatement
boilerplate
String sql = "INSERT INTO tip (tip_creator, symbol, target_price, reason) VALUES (?, ?, ?, ?)";
StockDatabase dbHelper = new StockDatabase(this.getApplicationContext());
SQLiteDatabase db = dbHelper.openDatabase();
SQLiteStatement stmt = db.compileStatement(sql);
stmt.bindString(1, "USERNAME");
stmt.bindString(2, symbol);
stmt.bindDouble(3, targetPrice);
stmt.bindString(4, reason);
stmt.execute();
stmt.close();
db.close();
14. TITLE : Memory 관리
REFERENCE OBJECT
1. Strong Reference
Strong Reference는 new를 이용해 생성된 객체는 GC에서 무조건 제외되기 때문에 메모리 누수(Memory Leak)을
방지하기 위해서는 이 참조를 주의해서 보아야 합니다. 이 참조를 지워주지 않으면 GC에 의해 수거되지 않고 남아
있기 때문에 메모리 누수를 유발할 수 있습니다.
2. Soft Reference
SoftReference는 다음과 같이 객체에 Reference를 넘겨줌으로서 생성할 수 있습니다.
SoftReference object = new SoftReference(new Object()); 이 참조는 메모리가 충분하다면 GC에 의해서 수거되지
않고, 메모리에 여유가 없다면 GC에 의해 수거됩니다.
3. Weak Reference
WeakReference는 SoftReference와 유사하지만 GC가 발생되기 전까지는 참조를 유지하고 GC가 발생하면 무조건
회수된다는 점에서 차이가 있습니다. 이는 유용하게 쓰일 수 있는데, 짧은 시간, 자주 쓰일 수 있는 객체를 이용할
때 유용하게 사용될 수 있습니다.
15. TITLE : Memory 관리
class BitmapWorkerTask extends AsyncTask {
private final WeakReference imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView){
imageViewReference = new WeakReference(imageView);
}
@Override protected
Bitmap doInBackground( Integer... params){
data = param[0];
return decodeSampledBitmapFromResource( getResources(), data, 100, 100);
}
@Override protected void onPostExecute( Bitmap bitmap){
if( imageViewReference != null && bitmap != null){
final ImageViewReference imageView = imageViewReference.get();
if( imageView != null){
imageView.setImageBitmap( bitmap);
}
}
}
}
16. TITLE : Memory 관리
LruCache는 LinkedHashMap을 사용하여 최근에 사용된 object의 strong reference를 보관하고 있다가 정
해진 사이즈를 넘어가게 되면
가장 최근에 사용되지 않은 놈부터 쫓아내는 LRU 알고리즘을 사용하는 메모리 캐시다
Android.util.LruCache<K, V>
LruCache의 캐시사이즈를 정하기 위해서는 다음 요소들을 고려해야 한다.
1. 우리 앱에서 앞으로 메모리를 얼마나 써야 되는가 ?.
2. 얼마나 많은 이미지들이 한 화면에 보일 것인가?
3. 얼마나 많은 이미지들이 다음에 보여주기 위해 준비되어야 하는가?
4. 화면 해상도가 어떻게 되는가?
5. 각 이미지 마다 메모리를 얼마나 차지하는가?
6. 이미지는 얼마나 자주 액세스 되는가?
위에 내용을 고려해서 정해야 하며 어느정도 캐시사이즈가 적당한지 공식은 없고
앱의 메모리 사용량을 측정해보면서 적당히 정해야 한다.
당연한 말이겠지만 너무 사이즈를 작게 하면 괜히 오버헤드만 발생하게 되고
사이즈를 너무 크게 했다가 OutOfMemory Exception을 보게 될 거다.
많은 양의 저해상도 이미지를 미리 보여주고 백그라운드로 고해상도 이미지를 로드 하는 방법이 좋을 수도 있다.
17. TITLE : Memory 관리
Android.util.LruCache<K, V>
private LruCache mMemoryCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
final int memClass = ((ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE)).getMemoryClass();
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 8;
mMemoryCache = new LruCache(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than number of items.
return bitmap.getByteCount();
}
};
...
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}