[Functions] Fix unbounded memory growth in FunctionLibraryRuntime.
authorDerek Murray <mrry@google.com>
Wed, 9 May 2018 16:42:18 +0000 (09:42 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Wed, 9 May 2018 17:52:05 +0000 (10:52 -0700)
commit16986a1c9ed64c2312ededf733f20a137b521819
tree1b3f281941851a8c51cfe77a82df4fc574dfff88
parentbcec296af809947145a6ebfa1e46b1cafe21ec06
[Functions] Fix unbounded memory growth in FunctionLibraryRuntime.

A recent change modified the behavior of `FunctionLibraryRuntimeImpl::ReleaseHandle()`
so that it no longer freed the memory associated with an instantiated function. Since
we rely on instantiating and releasing a potentially large number of instances of the
same function in tf.data to isolate the (e.g. random number generator) state in each
instance, this change meant that the memory consumption could grow without bound in
a simple program like:

```python

  ds = tf.data.Dataset.from_tensors(0).repeat(None)

  # The function `lambda y: y + 1` would be instantiated for each element in the input.
  ds = ds.flat_map(lambda x: tf.data.Dataset.from_tensors(x).map(
    lambda y: y + tf.random_uniform([], minval=0, maxval=10, dtype=tf.int32)))

  iterator = ds.make_one_shot_iterator()
  next_elem = iterator.get_next()

  with tf.Session() as sess:
    while True:
      sess.run(next_elem)
```

PiperOrigin-RevId: 195983977
tensorflow/core/common_runtime/function.cc
tensorflow/core/common_runtime/function_test.cc
tensorflow/core/common_runtime/function_threadpool_test.cc
tensorflow/core/common_runtime/process_function_library_runtime.cc
tensorflow/core/common_runtime/process_function_library_runtime.h
tensorflow/core/common_runtime/process_function_library_runtime_test.cc