water
Class MemoryManager

java.lang.Object
  extended by water.MemoryManager

public abstract class MemoryManager
extends java.lang.Object

Manages memory assigned to key/value pairs. All byte arrays used in keys/values should be allocated through this class - otherwise we risking running out of java memory, and throw unexpected OutOfMemory errors. The theory here is that *most* allocated bytes are allocated in large chunks by allocating new Values - with large backing arrays. If we intercept these allocation points, we cover most Java allocations. If such an allocation might trigger an OOM error we first free up some other memory. MemoryManager monitors memory used by the K/V store (by walking through the store (see Cleaner) and overall heap usage by hooking into gc. Memory is freed if either the cached memory is above the limit or if the overall heap usage is too high (in which case we want to use less mem for cache). There is also a lower limit on the amount of cache so that we never delete all the cache and therefore some computation should always be able to progress. The amount of memory to be freed is determined as the max of cached mem above the limit and heap usage above the limit. One of the primary control inputs is FullGC cycles: we check heap usage and set guidance for cache levels. We assume after a FullGC that the heap only has POJOs (Plain Old Java Objects, unknown size) and K/V Cached stuff (counted by us). We compute the free heap as MEM_MAX-heapUsage (after GC), and we compute POJO size as (heapUsage - K/V cache usage).


Field Summary
static long MEM_MAX
           
 
Constructor Summary
MemoryManager()
           
 
Method Summary
static byte[] arrayCopyOf(byte[] orig, int sz)
           
static int[] arrayCopyOf(int[] orig, int sz)
           
static long[] arrayCopyOf(long[] orig, int sz)
           
static byte[] arrayCopyOfRange(byte[] orig, int from, int sz)
           
static int[] arrayCopyOfRange(int[] orig, int from, int sz)
           
static long[] arrayCopyOfRange(long[] orig, int from, int sz)
           
static boolean canAlloc()
           
static void freeTaskMem(long m)
          Free the memory successfully reserved by task.
static java.lang.Object malloc(int elems, long bytes, int type, java.lang.Object orig, int from)
           
static byte[] malloc1(int size)
           
static short[] malloc2(int size)
           
static int[] malloc4(int size)
           
static float[] malloc4f(int size)
           
static long[] malloc8(int size)
           
static double[] malloc8d(int size)
           
static boolean[] mallocZ(int size)
           
static void reserveTaskMem(long m)
           
static void set_goals(java.lang.String msg, boolean oom)
           
static void set_goals(java.lang.String msg, boolean oom, long bytes)
           
static void setMemGood()
           
static void setMemLow()
           
static boolean tryReserveTaskMem(long m)
          Try to reserve memory needed for task execution and return true if succeeded.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MEM_MAX

public static final long MEM_MAX
Constructor Detail

MemoryManager

public MemoryManager()
Method Detail

setMemGood

public static void setMemGood()

setMemLow

public static void setMemLow()

canAlloc

public static boolean canAlloc()

set_goals

public static void set_goals(java.lang.String msg,
                             boolean oom)

set_goals

public static void set_goals(java.lang.String msg,
                             boolean oom,
                             long bytes)

malloc

public static java.lang.Object malloc(int elems,
                                      long bytes,
                                      int type,
                                      java.lang.Object orig,
                                      int from)

malloc1

public static byte[] malloc1(int size)

malloc2

public static short[] malloc2(int size)

malloc4

public static int[] malloc4(int size)

malloc8

public static long[] malloc8(int size)

malloc4f

public static float[] malloc4f(int size)

malloc8d

public static double[] malloc8d(int size)

mallocZ

public static boolean[] mallocZ(int size)

arrayCopyOfRange

public static byte[] arrayCopyOfRange(byte[] orig,
                                      int from,
                                      int sz)

arrayCopyOfRange

public static int[] arrayCopyOfRange(int[] orig,
                                     int from,
                                     int sz)

arrayCopyOfRange

public static long[] arrayCopyOfRange(long[] orig,
                                      int from,
                                      int sz)

arrayCopyOf

public static byte[] arrayCopyOf(byte[] orig,
                                 int sz)

arrayCopyOf

public static int[] arrayCopyOf(int[] orig,
                                int sz)

arrayCopyOf

public static long[] arrayCopyOf(long[] orig,
                                 int sz)

tryReserveTaskMem

public static boolean tryReserveTaskMem(long m)
Try to reserve memory needed for task execution and return true if succeeded. Tasks have a shared pool of memory which they should ask for in advance before they even try to allocate it. This method is another backpressure mechanism to make sure we do not exhaust system's resources by running too many tasks at the same time. Tasks are expected to reserve memory before proceeding with their execution and making sure they release it when done.

Parameters:
m - - requested number of bytes
Returns:
true if there is enough free memory

reserveTaskMem

public static void reserveTaskMem(long m)

freeTaskMem

public static void freeTaskMem(long m)
Free the memory successfully reserved by task.

Parameters:
m -