class MemoryServiceProxy extends ServiceProxy {

  public void getData(MemoryProfiler.MemoryRequest request, StreamObserver<MemoryProfiler.MemoryData> responseObserver) {
    try {
      MemoryProfiler.MemoryData data = myServiceStub.getData(request);

      if (myUseLegacyTracking) {
        synchronized (myUpdatingDataLock) {
          Long2ObjectMap<AllocationsInfo> infos = myTrackingInfos.get(request.getSession().getSessionId());
          MemoryProfiler.MemoryData.Builder rebuilder = data.toBuilder();
          long requestStartTime = request.getStartTime();
          long requestEndTime = request.getEndTime();

          // Note - the following is going to continuously return any unfinished whose start times are before the request's end time.
          // Dedeup is handled in MemoryDataPoller.
          List<AllocationsInfo> infosToReturn = new ArrayList<>();
          for (AllocationsInfo info : infos.values()) {
            if (info.getStartTime() <= requestEndTime && info.getEndTime() > requestStartTime) {
              infosToReturn.add(info);
            }
          }

          infosToReturn.sort(Comparator.comparingLong(AllocationsInfo::getStartTime));
          for (AllocationsInfo info : infosToReturn) {
            rebuilder.addAllocationsInfo(info);
            long infoMax = info.getEndTime() == Long.MAX_VALUE ? info.getStartTime() : info.getEndTime();
            rebuilder.setEndTimestamp(Math.max(rebuilder.getEndTimestamp(), infoMax));
          }

          data = rebuilder.build();
        }
      }

      responseObserver.onNext(data);
      responseObserver.onCompleted();
    }
    catch (RuntimeException e) {
      responseObserver.onError(e);
    }
  }

  public  MemoryServiceProxy(@NotNull IDevice device,
                            @NotNull ManagedChannel channel,
                            @NotNull Executor fetchExecutor,
                            @NotNull BiFunction<IDevice, Integer, LegacyAllocationTracker> legacyTrackerSupplier,
                            Map<String, ByteString> proxyBytesCache);

}