18. public class BuzzwordDetector {
static String[] prefixes = { "synergy", "win-win" };
static String[] myArgs = { "clown synergy", "gorilla win-wins", "whamee" };
!
public static void main(String[] args) {
args = myArgs;
!
}
}
int buzzwords = 0;
for (int i = 0; i < args.length; i++) {
String lc = args[i].toLowerCase();
for (int j = 0; j < prefixes.length; j++) {
if (lc.contains(prefixes[j])) {
buzzwords++;
}
}
}
System.out.println("Found " + buzzwords + " buzzwords");
19. New():
ref <- allocate()
if ref = null
collect()
ref <- allocate()
if ref = null
error "Out of memory"
return ref
/* Heap is full */
/* Heap is still full */
atomic collect():
markFromRoots()
sweep(HeapStart, HeapEnd)
From Garbage Collection Handbook
20. markFromRoots():
initialise(worklist)
for each fld in Roots
ref <- *fld
if ref != null && not isMarked(ref)
setMarked(ref)
add(worklist, ref)
mark()
initialise(worklist):
worklist <- empty
mark():
while not isEmpty(worklist)
ref <- remove(worklist)
for each fld in Pointers(ref)
child <- *fld
if (child != null && not isMarked(child)
setMarked(child)
add(worklist, child)
/* ref is marked */
From Garbage Collection Handbook
51. Server lies to clients about availability
TCP socket receive buffer
TCP write buffer
52. “Banner” protocol
1. Connect via TCP
2. Wait ~1-10ms
3. Either receive magic four byte header or try another host
4. Only send query after receiving header from server
71. #!/usr/bin/env bash
!
# This script is designed to be run every minute by cron.
!
host=$(hostname -s)
!
psout=$(ps h -p `cat /var/run/etsy-search.pid` -o min_flt,maj_flt 2>/dev/null)
min_flt=$(echo $psout | awk '{print $1}')
# minor page faults
maj_flt=$(echo $psout | awk '{print $2}')
# major page faults
!
epoch_s=$(date +%s)
!
echo -e "search_memstats.$host.etsy-search.min_fltt${min_flt:-0}t$epoch_s" | nc
graphite.etsycorp.com 2003
echo -e "search_memstats.$host.etsy-search.maj_fltt${maj_flt:-0}t$epoch_s" | nc
graphite.etsycorp.com 2003
72. Solution 1: Buy more RAM
~$5-10/GB
Ideally enough RAM to:
Keep index in OS file buffers
AND ensure no paging of VM memory
AND whatever else happens on the box