import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
public final class ActivityMemoryMonitor
{
private final static ReferenceQueue<Activity> referenceQueue = new ReferenceQueue<Activity>();
private final static Map<PhantomReference<Activity>, String> phantomReferenceMap = new HashMap<PhantomReference<Activity>, String>();
private final static String logFile = "activity_memory_monitor.log";
private static Timer memoryMonitorTimer = null;
public static void setActivityToMonitoringQueue(Activity activity)
{
String activityInfo = activity.toString() + "." + activity.hashCode();
phantomReferenceMap.put(new PhantomReference<Activity>(activity, referenceQueue), activityInfo);
writeLogToFile(activity, "Activity Created: " + activityInfo);
startMonitoringIfNeeded(activity.getApplicationContext());
}
private static void startMonitoringIfNeeded(Context context)
{
if (memoryMonitorTimer != null)
{
return;
}
memoryMonitorTimer = new Timer();
memoryMonitorTimer.schedule(new MemoryMonitorTask(context), 0, 2000);
}
private static synchronized void writeLogToFile(Context context, String message)
{
FileOutputStream fos = null;
BufferedOutputStream out = null;
try
{
fos = context.openFileOutput(logFile, Context.MODE_APPEND);
out = new BufferedOutputStream(fos);
String log = new Date().toString() + " " + message + "\n";
out.write(log.getBytes());
} catch (IOException e)
{
} finally
{
close(out);
close(fos);
}
}
private static void close(Closeable c)
{
if (c == null)
{
return;
}
try
{
c.close();
} catch (IOException e)
{
}
}
private static class MemoryMonitorTask extends TimerTask
{
private Context context;
MemoryMonitorTask(Context context)
{
this.context = context;
}
@Override
public void run()
{
System.gc();
Object o = ActivityMemoryMonitor.referenceQueue.poll();
if (o != null)
{
writeLogToFile(context, "GC Cllected: " + phantomReferenceMap.get(o));
phantomReferenceMap.remove(o);
}
}
}
}
参考文章:http://www.iteye.com/topic/1117008
引出几个知识点:
Java 中一共有 4 种类型的引用 : StrongReference、 SoftReference、 WeakReference 以及 PhantomReference (传说中的幽灵引用 呵呵),
详细查看:http://www.douban.com/note/155272515/
小知识点:在内存中写文件
-
privatestaticsynchronizedvoidwriteLogToFile(Contextcontext,Stringmessage)
-
{
-
FileOutputStreamfos=null;
-
BufferedOutputStreamout=null;
-
try
-
{
-
fos=context.openFileOutput(logFile,Context.MODE_APPEND); //写在手机内存中
-
out=newBufferedOutputStream(fos);
-
Stringlog=newDate().toString()+""+message+"\n";
-
out.write(log.getBytes());
-
}catch(IOExceptione)
-
{
-
}finally
-
{
-
close(out);
-
close(fos);
-
}
-
}
分享到:
相关推荐
私人泄密调查 用于演示 AngularJS 内存泄漏的示例应用程序
V2EX 克隆项目升级 Vue.js 1.x to 2.0
关键字: 内存泄漏 checkMem.pas 摘要: 这是一篇介绍如何使用CheckMem.pas单元检查delphi应用程序内存泄漏的文章 作者:999roseto347(fdaf at 163 dot com) 版本:V1.0 创建日期:2004-06-11 目录...
Gradient Inversion Attack Leaking Private Labels in Two-Party Split Learning_两方分裂学习中泄漏私有标签的梯度反转攻击.pdf
Cisco_Fusion_Router_IOS_XE_route-leaking_guide from yuanshou
用于根据输入的单词或句子生成多个句子建议。 有关项目详细信息的更多信息,请参见与此项目相关联的。 响应:仅生成1个私钥,即生成描述的单词数 Input: hydrant requires repair Output: hydrant requires repair ...
信息安全_数据安全_asec-t08-leaking-ads-is-user-data-truly-secure 金融安全 安全研究 安全编码 安全审计 漏洞挖掘
loki-rule-operator Loki-rule-operator是轻量级的Kubernetes运算符,可将LokiRule和GlobalLokiRule自定义资源定义添加到集群中。 这些资源有助于为您的配置。例子apiVersion : logging.opsgy.com/v1beta1kind : ...
Snooper.cfc-用于查找实时变量泄漏的ColdFusion组件由 (在) 这是一个很小的ColdFusion组件,作为健全性检查,我将使用它检查在生产系统中缓存的其他ColdFusion组件。 Snooper.cfc通过注入可以访问目标组件的私有...
System working set paging activity can be viewed by monitoring the Memory: Cache Faults/sec counter. For more information, see also… Inside Windows 2000,Third Edition, p. 463. System Cache The ...
它旨在通过提供基于自动梯度的超参数调整(岭回归惩罚、谱半径、泄漏率和反馈缩放)以及瞬态时间估计的算法,使ESN的使用尽可能简单。 此外,它结合了使用时空ESN进行几何扩展输入信号的预测和分类的能力。 ...
HITCON CTF 2017 所有题目整理...............................................................................................................................................................................
2012 年 02 月 06 日,一位 Mozilla 的工程师 lloyd 创建了这个库,并写了一篇博文“Is My NodeJS Program Leaking?”(我的 Node.js 程序是否存在内存泄漏?)。这个包最初被命名为 gcstats,代码上传到了 gith
Application auditing avoids back door program leaking sensitive information, and genearates application filter rule intelligentlly. Particular filter rules of ...
剥皮机 讲故事给您泄露秘密! Squealer扫描本地git存储库,以查找在提交历史记录深处泄露的机密。 内置配置具有以下检查...Telling tales on your secret leaking Usage: squealer [flags] Flags: --concise Reduc
Fragment or Activity anymore while your background work finishes. How to use it? The first thing to do is to initialize Tesela providing it a TeselaExecutor implementation. That way you can control ...
Volley是一个优秀的安卓开源网络访问工具,这里包含了volley jar包和源码,版本是2015.07.28的1.0.18版本。更多资料可以参见volley的github地址: https://github.com/mcxiaoke/android-volley 1.0.18 2015.07....
注意:由于Go的作用域很广,因此必须避免使用与标记和释放调用相同作用域的新变量。例行程序如果您想知道某个函数是否泄漏goroutine: leaks := leak . GoRoutineLeaks ( foo )if leaks > 0 {panic ( "foo is ...
Wi-Fi Privacy Police prevents your smartphone or tablet from leaking privacy sensitive information via Wi-Fi networks. It does this in two ways: It prevents your smartphone from sending out the names...
Nowadays, people are afraid of the leaking of their secrets result from deleted file recovery more and more because they depend on computers more and more. Today, Network and Internet bring more ...