Pool:小对象缓存or复用
对象复用
使用链表作为pool来保存要复用的对象。
- pool字段
- obtain
- recycle
案例1
android.os.Message
private static Message sPool;
Message msg = mHandler.obtainMessage(MSG_PROCESS_INPUT_EVENTS);
/**
* Return a new Message instance from the global pool. Allows us to
* avoid allocating new objects in many cases.
*/
public static Message obtain() {
synchronized (sPoolSync) {
if (sPool != null) {
Message m = sPool;
sPool = m.next;
m.next = null;
m.flags = 0; // clear in-use flag
sPoolSize--;
return m;
}
}
return new Message();
}
案例2
android.view.ViewRootImpl
private QueuedInputEvent mQueuedInputEventPool;
QueuedInputEvent q = obtainQueuedInputEvent(event, receiver, flags);
private QueuedInputEvent obtainQueuedInputEvent(InputEvent event,
InputEventReceiver receiver, int flags) {
QueuedInputEvent q = mQueuedInputEventPool;
if (q != null) {
mQueuedInputEventPoolSize -= 1;
mQueuedInputEventPool = q.mNext;
q.mNext = null;
} else {
q = new QueuedInputEvent();
}
q.mEvent = event;
q.mReceiver = receiver;
q.mFlags = flags;
return q;
}
案例3
android.view.MotionEvent
private static MotionEvent gRecyclerTop;
static private MotionEvent obtain() {
final MotionEvent ev;
synchronized (gRecyclerLock) {
ev = gRecyclerTop;
if (ev == null) {
return new MotionEvent();
}
gRecyclerTop = ev.mNext;
gRecyclerUsed -= 1;
}
ev.mNext = null;
ev.prepareForReuse();
return ev;
}
/**
* Recycle the MotionEvent, to be re-used by a later caller. After calling
* this function you must not ever touch the event again.
*/
@Override
public final void recycle() {
super.recycle();
synchronized (gRecyclerLock) {
if (gRecyclerUsed < MAX_RECYCLED) {
gRecyclerUsed++;
mNext = gRecyclerTop;
gRecyclerTop = this;
}
}
}
Pool:小对象缓存or复用的更多相关文章
- Java小对象的解决之道——对象池(Object Pool)的设计与应用
一.概述 面向对象编程是软件开发中的一项利器,现已经成为大多数编程人员的编程思路.很多高级计算机语言也对这种编程模式提供了很好的支持,例如C++.Object Pascal.Java等.曾经有大量的软 ...
- 高性能的分布式内存对象缓存系统Memcached
Memcached概述 什么是Memcached? 先看看下面几个概念: Memory:内存存储,不言而喻,速度快,对于内存的要求高,不指出的话所缓存的内容非持久化.对于CPU要求很低,所以常常采 ...
- 分布式内存对象缓存系统Memcached-概述
全面掌握Memcached 1. 概述 Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,是为了加快网站http://www. ...
- MemCache分布式内存对象缓存系统
MemCache超详细解读 MemCache是一个自由.源码开放.高性能.分布式的分布式内存对象缓存系统,用于动态Web应用以减轻数据库的负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而 ...
- Python并发编程-Memcached (分布式内存对象缓存系统)
一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...
- PHPExcel解决内存占用过大问题-设置单元格对象缓存
PHPExcel解决内存占用过大问题-设置单元格对象缓存 PHPExcel是一个很强大的处理Excel的PHP开源类,但是很大的一个问题就是它占用内存太大,从1.7.3开始,它支持设置cell的缓存方 ...
- 【笔记】对golang的大量小对象的管理真的是无语了……
业务中有这样一个struct: type bizData struct{ A uint64 B uint64 C int32 D uint32 } 虽然没有实测,但我猜测这样的对齐方式,这个struc ...
- spring ehcache 页面、对象缓存
一.Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.g ...
- Ehcache 整合Spring 使用页面、对象缓存
Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...
随机推荐
- project5 大数据
[概念] build和run不是一样的,要看输出,不要误解. [方法论] 先要搞懂每个方法的使用,不能乱写.文件名不要写成字符串内容. 又忘记用lab code了,该死. programcreek是个 ...
- sqlserver 3145
参考链接:http://www.uoften.com/dbs/mssql2005/20180415/73780.html 第一步:查询 use master;--用此语句得到备份文件的逻辑文件名RES ...
- jmeter javamail 邮件格式再优化(由详情——>改为统计)
前言:之前扩展的ant—jmeter支持邮件附件形式上传以及邮件内容的html文件格式. 如图: 由于邮件的内容格式是详情信息,也就是说直观的显示的是case,但由于case的增加,邮件内容越来越大! ...
- Docker 日志都在哪里?怎么收集?
https://www.cnblogs.com/YatHo/p/7866029.html 日志分两类,一类是 Docker 引擎日志:另一类是 容器日志. Docker 引擎日志 Docker 引擎日 ...
- Google关于Spanner的论文中分布式事务的实现
Google关于Spanner的论文中分布式事务的实现 Google在Spanner相关的论文中详细的解释了Percolator分布式事务的实现方式, 而且用简洁的伪代码示例怎么实现分布式事务; Pe ...
- 博客1--tensorflow的图像基本处理操作
话不多,具体内容在开源中国里我的博客:https://my.oschina.net/u/3770644 代码: #!/usr/bin/env python# -*- coding:utf-8 -*-# ...
- 关于python的感想和turtle作图
第一次知道有python这个东西是在去年七月份,就是一堆编程语言,当时我想它应该离我很远,也没对它有什么兴趣.直到在三月份的某一天我真正接触了它,其实也不是我主动,环境下学期必须学这东西.我有它的必修 ...
- Spring 缓存注解之@Cacheable,@CacheEvit
Spring引入了Cache的支持,其使用方法类似于Spring对事务的支持.Spring Cache是作用于方法上的,其核心思想是,当我们调用一个缓存方法时,把该方法参数和返回结果作为一个键值对存放 ...
- Maven学习 九 maven热部署
第一步:配置tomcat的manager-script角色 点击tomcat的默认项目root的欢迎页面的Manager App 刚开始是没有用户名与和密码的,直接点击取消 出现如下的一张图片,图片中 ...
- Innodb 状态的部分解释
Innodb_buffer_pool_pages_data Innodb buffer pool缓存池中包含数据的页的数目,包括脏页.单位是page. Innodb_buffer_pool_pages ...