AI2 Component  (Version nb184)
MemoryLeakUtil.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
10 
11 import android.util.Log;
12 
13 import java.lang.ref.WeakReference;
14 import java.util.Iterator;
15 import java.util.Map;
16 import java.util.concurrent.atomic.AtomicInteger;
17 
23 public class MemoryLeakUtil {
24  private static final String LOG_TAG = "MemoryLeakUtil";
25 
26  private static final AtomicInteger prefixGenerator = new AtomicInteger(0);
27  private static final Map<String, WeakReference<Object>> TRACKED_OBJECTS = Maps.newTreeMap();
28 
29  private MemoryLeakUtil() {
30  }
31 
40  public static String trackObject(String tag, Object object) {
41  String key = (tag == null)
42  ? prefixGenerator.incrementAndGet() + "_"
43  : prefixGenerator.incrementAndGet() + "_" + tag;
44  TRACKED_OBJECTS.put(key, new WeakReference<Object>(object));
45  return key;
46  }
47 
57  public static boolean isTrackedObjectCollected(String key, boolean stopTrackingIfCollected) {
58  System.gc();
59  WeakReference<Object> ref = TRACKED_OBJECTS.get(key);
60  if (ref != null) {
61  Object o = ref.get();
62  String tag = key.substring(key.indexOf("_") + 1);
63  Log.i(LOG_TAG, "Object with tag " + tag + " has " +
64  ((o != null) ? "not " : "") + "been garbage collected.");
65  if (stopTrackingIfCollected && o == null) {
66  TRACKED_OBJECTS.remove(key);
67  }
68  return o == null;
69  }
70  throw new IllegalArgumentException("key not found");
71  }
72 
82  public static void checkAllTrackedObjects(boolean verbose, boolean stopTrackingCollectedObjects) {
83  Log.i(LOG_TAG, "Checking Tracked Objects ----------------------------------------");
84  System.gc();
85  int countRemaining = 0;
86  int countCollected = 0;
87  for (Iterator<Map.Entry<String, WeakReference<Object>>> it =
88  TRACKED_OBJECTS.entrySet().iterator(); it.hasNext();) {
89  Map.Entry<String, WeakReference<Object>> entry = it.next();
90  String key = entry.getKey();
91  WeakReference<Object> ref = entry.getValue();
92  Object o = ref.get();
93  if (o != null) {
94  countRemaining++;
95  } else {
96  countCollected++;
97  if (stopTrackingCollectedObjects) {
98  it.remove();
99  }
100  }
101  if (verbose) {
102  String tag = key.substring(key.indexOf("_") + 1);
103  Log.i(LOG_TAG, "Object with tag " + tag + " has " +
104  ((o != null) ? "not " : "") + "been garbage collected.");
105  }
106  }
107  Log.i(LOG_TAG, "summary: collected " + countCollected);
108  Log.i(LOG_TAG, "summary: remaining " + countRemaining);
109  Log.i(LOG_TAG, "-----------------------------------------------------------------");
110  }
111 }
com.google.appinventor.components
com.google.appinventor.components.runtime.collect
Definition: Lists.java:7
com.google.appinventor.components.runtime.collect.Maps
Definition: Maps.java:19
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Map
Definition: Map.java:84
com.google.appinventor.components.runtime.util.MemoryLeakUtil.isTrackedObjectCollected
static boolean isTrackedObjectCollected(String key, boolean stopTrackingIfCollected)
Definition: MemoryLeakUtil.java:57
com.google
com
com.google.appinventor.components.runtime.MapFeatureContainerBase.iterator
Iterator< MapFeature > iterator()
Definition: MapFeatureContainerBase.java:347
com.google.appinventor.components.runtime.collect.Maps.newTreeMap
static< K, V > TreeMap< K, V > newTreeMap()
Definition: Maps.java:34
com.google.appinventor.components.runtime.util.MemoryLeakUtil.checkAllTrackedObjects
static void checkAllTrackedObjects(boolean verbose, boolean stopTrackingCollectedObjects)
Definition: MemoryLeakUtil.java:82
com.google.appinventor
com.google.appinventor.components.runtime.util.MemoryLeakUtil.trackObject
static String trackObject(String tag, Object object)
Definition: MemoryLeakUtil.java:40
com.google.appinventor.components.runtime.util.MemoryLeakUtil
Definition: MemoryLeakUtil.java:23