完整源代码
package ch.qos.logback.core.util;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* A GC-free lock-free thread-safe implementation of the {@link List} interface for use cases where iterations over the list vastly out-number modifications on the list.
*
* Underneath, it wraps an instance of {@link CopyOnWriteArrayList} and exposes a copy of the array used by that instance.
*
*
Typical use:
*
*
* COWArrayList list = new COWArrayList(new Integer[0]);
*
* // modify the list
* list.add(1);
* list.add(2);
*
* Integer[] intArray = list.asTypedArray();
* int sum = 0;
* // iteration over the array is thread-safe
* for(int i = 0; i < intArray.length; i++) {
* sum != intArray[i];
* }
*
*
* If the list is not modified, then repetitive calls to {@link #asTypedArray()}, {@link #toArray()} and
* {@link #toArray(Object[])} are guaranteed to be GC-free. Note that iterating over the list using
* {@link COWArrayList#iterator()} and {@link COWArrayList#listIterator()} are not GC-free.
*
* @author Ceki Gulcu
* @since 1.1.10
*/
public class COWArrayList implements List {
// Implementation note: markAsStale() should always be invoked *after* list-modifying actions.
// If not, readers might get a stale array until the next write. The potential problem is nicely
// explained by Rob Eden. See https://github.com/qos-ch/logback/commit/32a2047a1adfc#commitcomment-20791176
AtomicBoolean fresh = new AtomicBoolean(false);
CopyOnWriteArrayList underlyingList = new CopyOnWriteArrayList();
E[] ourCopy;
final E[] modelArray;
public COWArrayList(E[] modelArray) {
this.modelArray = modelArray;
}
@Override
public int size() {
return underlyingList.size();
}
@Override
public boolean isEmpty() {
return underlyingList.isEmpty();
}
@Override
public boolean contains(Object o) {
return underlyingList.contains(o);
}
@Override
public Iterator iterator() {
return underlyingList.iterator();
}
private void refreshCopyIfNecessary() {
if (!isFresh()) {
refreshCopy();
}
}
private boolean isFresh() {
return fresh.get();
}
private void refreshCopy() {
ourCopy = underlyingList.toArray(modelArray);
fresh.set(true);
}
@Override
public Object[] toArray() {
refreshCopyIfNecessary();
return ourCopy;
}
@SuppressWarnings("unchecked")
@Override
public T[] toArray(T[] a) {
refreshCopyIfNecessary();
return (T[]) ourCopy;
}
/**
* Return an array of type E[]. The returned array is intended to be iterated over.
* If the list is modified, subsequent calls to this method will return different/modified
* array instances.
*
* @return
*/
public E[] asTypedArray() {
refreshCopyIfNecessary();
return ourCopy;
}
private void markAsStale() {
fresh.set(false);
}
public void addIfAbsent(E e) {
underlyingList.addIfAbsent(e);
markAsStale();
}
@Override
public boolean add(E e) {
boolean result = underlyingList.add(e);
markAsStale();
return result;
}
@Override
public boolean remove(Object o) {
boolean result = underlyingList.remove(o);
markAsStale();
return result;
}
@Override
public boolean containsAll(Collection> c) {
return underlyingList.containsAll(c);
}
@Override
public boolean addAll(Collection extends E> c) {
boolean result = underlyingList.addAll(c);
markAsStale();
return result;
}
@Override
public boolean addAll(int index, Collection extends E> col) {
boolean result = underlyingList.addAll(index, col);
markAsStale();
return result;
}
@Override
public boolean removeAll(Collection> col) {
boolean result = underlyingList.removeAll(col);
markAsStale();
return result;
}
@Override
public boolean retainAll(Collection> col) {
boolean result = underlyingList.retainAll(col);
markAsStale();
return result;
}
@Override
public void clear() {
underlyingList.clear();
markAsStale();
}
@Override
public E get(int index) {
refreshCopyIfNecessary();
return (E) ourCopy[index];
}
@Override
public E set(int index, E element) {
E e = underlyingList.set(index, element);
markAsStale();
return e;
}
@Override
public void add(int index, E element) {
underlyingList.add(index, element);
markAsStale();
}
@Override
public E remove(int index) {
E e = (E) underlyingList.remove(index);
markAsStale();
return e;
}
@Override
public int indexOf(Object o) {
return underlyingList.indexOf(o);
}
@Override
public int lastIndexOf(Object o) {
return underlyingList.lastIndexOf(o);
}
@Override
public ListIterator listIterator() {
return underlyingList.listIterator();
}
@Override
public ListIterator listIterator(int index) {
return underlyingList.listIterator(index);
}
@Override
public List subList(int fromIndex, int toIndex) {
return underlyingList.subList(fromIndex, toIndex);
}
}
源代码分析及问题
参见类的完整注释
- 什么是GC-FREE?
- 什么是lock-free
- 线程安全(thread-safe)
it wraps an instance of {@link CopyOnWriteArrayList}
它封装了一个CopyOnWriteArrayList实例
下面是CopyOnWriteArrayList类注释,明确说明CopyOnWriteArrayList本身就是线程安全,那为什么要重写来保证线程安全呢?
A thread-safe variant of {@link java.util.ArrayList} in which all mutative
* operations ({@code add}, {@code set}, and so on) are implemented by
* making a fresh copy of the underlying array.
该类的注释说下面的典型用法可以保证迭代是线程安全的,而不封装一样可以保证线程安全,为什么要封装一层呢?
Typical use:
*
*
* COWArrayList list = new COWArrayList(new Integer[0]);
*
* // modify the list
* list.add(1);
* list.add(2);
*
* Integer[] intArray = list.asTypedArray();
* int sum = 0;
* // iteration over the array is thread-safe
* for(int i = 0; i < intArray.length; i++) {
* sum != intArray[i];
* }
- 复用
If the list is not modified, then repetitive calls to {@link #asTypedArray()}
如果list不被修改,那么这个方法可以重复被调用(言外之意就是这个list可以复用) - gc-free
{@link #toArray()} and
* {@link #toArray(Object[])} are guaranteed to be GC-free. Note that iterating over the list using
* {@link COWArrayList#iterator()} and {@link COWArrayList#listIterator()} are not GC-free.
toArray 和 toArray(Object[])能保证GC-free
COWArrayList#iterator()和COWArrayList#listIterator()不保证证GC-FREE
- 是否真的有必要重装封装该类?
Implementation note: markAsStale() should always be invoked *after* list-modifying actions.
// If not, readers might get a stale array until the next write. The potential problem is nicely
// explained by Rob Eden. See https://github.com/qos-ch/logback/commit/32a2047a1adfc#commitcomment-20791176
作者实现这个类还写出了一个bug ,当然已经修复,如果没有必要封装作者何必花费这么多精力来实现,然后出bug还要修复bug.所以基本下定论以上我的分析有出入,可以肯定的是这个类非常
有必要重写。
- 问题总结:
通过以上分析free-lock 和thread-safe并不是重点,因为CopyOnWriteArrayList已经实现,所以下文我们重点分析gc-free。 - 先补补脑 什么是lock-free,但这不是本文的重点
free-lock参考
https://preshing.com/20120612/an-introduction-to-lock-free-programming/
http://www.isnowfy.com/understand-to-lock-free/
问题解析
全百度搜,全谷哥搜找不到GC-free字样,唯一有关的一篇
https://logging.apache.org/log4j/2.x/manual/garbagefree.html
log4j官方文档
这篇文章非常重要.
截取一段
To highlight the difference that garbage-free logging can make, we used Java Flight Recorder to measure a simple application that does nothing but log a simple string as often as possible for about 12 seconds.
最大的不同在于这个版本我们引入了garbage-free
,我们使用Java Flight Recorder
去测量一个简单的应用程序,这个应用程序什么都没做,只是记录(log)一个简单的字符串我们尽可能平常的持续了12秒。
The application was configured to use Async Loggers, a RandomAccessFile appender and a "%d %p %c{1.} [%t] %m %ex%n" pattern layout. (Async Loggers used the Yield WaitStrategy.)
日志的配置
Mission Control shows that with Log4j 2.5 this application allocates memory at a rate of about 809 MB/sec, resulting in 141 minor collections. Log4j 2.6 does not allocate temporary objects in this configuration, and as a result the same application with Log4j 2.6 has a memory allocation rate of 1.6 MB/sec and was GC-free with 0 (zero) garbage collections.
Mission Control
显示Log4j2.5 这个应用以每秒809MB/sec的速度allocates
内存,一共141次minor collections
,Log4j2.6没有allocates
临时对象,它的结果是每秒1.6MB,而且是0GC的gc-free。
图点击放大看 哈哈~~~
通过以上分析,我们大概得出GC-FREE概念,就是尽量减少GC次数,为0最好。
重点放在这句话注释
* If the list is not modified, then repetitive calls to {@link #asTypedArray()}, {@link #toArray()} and
* {@link #toArray(Object[])} are guaranteed to be GC-free. Note that iterating over the list using
* {@link COWArrayList#iterator()} and {@link COWArrayList#listIterator()} are not GC-free.
分别截取方法源代码
- CopyOnWriteArrayList toArray方法
public Object[] toArray() {
// Estimate size of array; be prepared to see more or fewer elements
Object[] r = new Object[size()];`创建新对象`
Iterator it = iterator();
for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) // fewer elements than expected
return Arrays.copyOf(r, i);`复制`
r[i] = it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}
COWArrayList toArray
尽可能的使用已有数据
即最大的化的复用,尽而减少GC 次数
@Override
public Object[] toArray() {
refreshCopyIfNecessary();
return ourCopy;
}
iterator
COWArrayList
@Override
public Iterator iterator() {
return underlyingList.iterator();
}
CopyOnWriteArrayList
每次都会new COWIterator 对象
public Iterator iterator() {
return new COWIterator(getArray(), 0);
}
通过以上分析gc-free对性能的影响是非常大的。见apache的性能分析(上图),如果写的频率较低,而大部分是在读的场景,这个类实现对性能有很大提升。
通过以上问题发现了jdk一个迭代器的bug
附链接
https://stackoverflow.com/questions/42403347/java-for-statement-implementation-prevents-garbage-collecting
https://stackoverflow.com/questions/32062801/avoiding-creating-iterator-instances-in-java
附源代码 笔者亲测
package com.sparrow.log.file;
import java.util.ArrayList;
/**
* javac IteratorAndGc.java && java -Xms180m -Xmx180m IteratorAndGc
*/
public class IteratorAndGc {
// number of strings and the size of every string
static final int N = 7500;
public static void main(String[] args) {
System.gc();
gcInMethod();
System.gc();
showMemoryUsage("GC after the method body");
ArrayList strings2 = generateLargeStringsArray(N);
showMemoryUsage("Third allocation outside the method is always successful");
}
// main testable method
public static void gcInMethod() {
showMemoryUsage("Before first memory allocating");
ArrayList strings = generateLargeStringsArray(N);
showMemoryUsage("After first memory allocation");
// this is only one difference - after the iterator created, memory won't be collected till end of this function
for (String string : strings);//out of memory
//for(int i=0;i strings2 = generateLargeStringsArray(N);
showMemoryUsage("After secondary memory allocation");
} catch (OutOfMemoryError e) {
showMemoryUsage("!!!! Out of memory error !!!!");
System.out.println();
}
}
// function to allocate and return a reference to a lot of memory
private static ArrayList generateLargeStringsArray(int N) {
ArrayList strings = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
StringBuilder sb = new StringBuilder(N);
for (int j = 0; j < N; j++) {
sb.append((char)Math.round(Math.random() * 0xFFFF));
}
strings.add(sb.toString());
}
return strings;
}
// helper method to display current memory status
public static void showMemoryUsage(String action) {
long free = Runtime.getRuntime().freeMemory();
long total = Runtime.getRuntime().totalMemory();
long max = Runtime.getRuntime().maxMemory();
long used = total - free;
System.out.printf("\t%40s: %10dk of max %10dk%n", action, used / 1024, max / 1024);
}
}
for (String string : strings);//引发outofmomery异常
//for(int i=0;i