這是一個背景記錄器,它記錄時間/日期和一個StringBuilder完全記錄在後臺。典型的延遲低於每次通話1微秒。它回收所有東西,所以不會產生GC。
這比使用隊列在兩個線程之間傳遞工作要高效得多。不幸的是,所有隊列實現都會創建垃圾。 :(
import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class BackgroundLogger implements Runnable {
static final int ENTRIES = 64;
static class LogEntry {
long time;
int level;
final StringBuilder text = new StringBuilder();
}
static class LogEntries {
final LogEntry[] lines = new LogEntry[ENTRIES];
int used = 0;
}
private final ExecutorService executor = Executors.newSingleThreadExecutor();
final Exchanger<LogEntries> logEntriesExchanger = new Exchanger<LogEntries>();
LogEntries entries = new LogEntries();
BackgroundLogger() {
executor.submit(this);
}
// put whatever you want in the StringBuilder, before the next call!
public StringBuilder log(int level) {
try {
if (entries.used == ENTRIES)
entries = logEntriesExchanger.exchange(entries);
LogEntry le = entries.lines[entries.used++];
le.time = System.currentTimeMillis();
le.level = level;
return le.text;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void flush() throws InterruptedException {
entries = logEntriesExchanger.exchange(entries);
entries = logEntriesExchanger.exchange(entries);
}
public void stop() {
try {
flush();
} catch (InterruptedException e) {
e.printStackTrace();
}
executor.shutdownNow();
}
@Override
public void run() {
LogEntries entries = new LogEntries();
try {
while(!Thread.interrupted()) {
entries = logEntriesExchanger.exchange(entries);
for (int i = 0; i < entries.used; i++) {
bgLog(entries.lines[i]);
entries.lines[i].text.delete(0, entries.lines[i].text.length());
}
entries.used = 0;
}
} catch (InterruptedException ignored) {
} finally {
System.out.println("logger stopping.");
}
}
private void bgLog(LogEntry line) {
// log the entry to a file.
}
}
我寫了一個。
如果允許的垃圾一點點,你可以簡化你的問題。你可以使用SimpleDateFormatter每次改變時間格式的日期(即一旦注意:通過創建一個字符串,你仍然在生產垃圾(一個String和一個char [],即使你沒有使用一個StringBuilder,這是非常棘手的)。
我會附加到一個循環的ByteBuffer以避免任何GC。 (除了在午夜)
作爲@Joachim碟表明格式化可用於產生較少的垃圾。我懷疑,除非你也放棄生產一個字符串,它不會有太大的區別。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
StringBuffer sb = new StringBuffer();
Date tmpDate = new Date();
final FieldPosition pos = new FieldPosition(0);
{
long free1 = Runtime.getRuntime().freeMemory();
for (int i = 0; i < 1000; i++) {
tmpDate.setTime(System.currentTimeMillis());
sdf.format(tmpDate, sb, pos);
sb.delete(0, sb.length());
}
long free2 = Runtime.getRuntime().freeMemory();
if (free1 == free2) throw new Error("This must be run with -XX:-UseTLAB");
System.out.println("SDF.format used an average of " + (free1 - free2)/1000 + " bytes");
}
{
long free1 = Runtime.getRuntime().freeMemory();
for (int i = 0; i < 1000; i++) {
tmpDate.setTime(System.currentTimeMillis());
sdf.format(tmpDate, sb, pos);
String str = sb.toString();
sb.delete(0, sb.length());
}
long free2 = Runtime.getRuntime().freeMemory();
if (free1 == free2) throw new Error("This must be run with -XX:-UseTLAB");
System.out.println("SDF.format with a String used an average of " + (free1 - free2)/1000 + " bytes");
}
打印
SDF.format used an average of 24 bytes
SDF.format with a String used an average of 120 bytes
如果您不想產生垃圾,請勿使用垃圾收集語言。你爲什麼關心SimpleDateFormat產生的垃圾? –
@JB Nizet:我沒有這個選項。我必須使用Java,並且必須儘量減少GC延遲。這對於低延遲行業來說並不罕見。 – TraderJoeChicago
@JB Nizet,如果您的應用程序少於1%需要不使用GC,您是否會更改其他99%以滿足此要求? –