7 package com.google.appinventor.components.runtime.util;
11 import android.util.Log;
13 import java.lang.ref.WeakReference;
14 import java.util.Iterator;
16 import java.util.concurrent.atomic.AtomicInteger;
24 private static final String LOG_TAG =
"MemoryLeakUtil";
26 private static final AtomicInteger prefixGenerator =
new AtomicInteger(0);
41 String key = (tag ==
null)
42 ? prefixGenerator.incrementAndGet() +
"_"
43 : prefixGenerator.incrementAndGet() +
"_" + tag;
44 TRACKED_OBJECTS.put(key,
new WeakReference<Object>(
object));
59 WeakReference<Object> ref = TRACKED_OBJECTS.get(key);
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);
70 throw new IllegalArgumentException(
"key not found");
83 Log.i(LOG_TAG,
"Checking Tracked Objects ----------------------------------------");
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();
97 if (stopTrackingCollectedObjects) {
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.");
107 Log.i(LOG_TAG,
"summary: collected " + countCollected);
108 Log.i(LOG_TAG,
"summary: remaining " + countRemaining);
109 Log.i(LOG_TAG,
"-----------------------------------------------------------------");