MemoryProfiler extends StudioProfiler { public static void saveHeapDumpToFile(@NotNull ProfilerClient client, @NotNull Common.Session session, @NotNull HeapDumpInfo info, @NotNull OutputStream outputStream, @NotNull FeatureTracker featureTracker) { Transport.BytesResponse response = client.getTransportClient() .getBytes(Transport.BytesRequest.newBuilder().setStreamId(session.getStreamId()).setId(Long.toString(info.getStartTime())).build()); if (response.getContents() != ByteString.EMPTY) { try { response.getContents().writeTo(outputStream); featureTracker.trackExportHeap(); } catch (IOException exception) { getLogger().warn("Failed to export heap dump file:\n" + exception); } } } MemoryProfiler(@NotNull StudioProfilers profilers); @Override ProfilerMonitor newMonitor(); @Override void startProfiling(Common.Session session); @Override void stopProfiling(Common.Session session); static boolean hasOnlyFullAllocationTrackingWithinRegion(@NotNull StudioProfilers profilers,
                                                                  @NotNull Common.Session session, long startTimeUs, long endTimeUs); static void saveHeapDumpToFile(@NotNull ProfilerClient client,
                                        @NotNull Common.Session session,
                                        @NotNull HeapDumpInfo info,
                                        @NotNull OutputStream outputStream,
                                        @NotNull FeatureTracker featureTracker); static void saveLegacyAllocationToFile(@NotNull ProfilerClient client,
                                                @NotNull Common.Session session,
                                                @NotNull AllocationsInfo info,
                                                @NotNull OutputStream outputStream,
                                                @NotNull FeatureTracker featureTracker); static List<HeapDumpInfo> getHeapDumpsForSession(@NotNull ProfilerClient client,
                                                          @NotNull Common.Session session,
                                                          @NotNull Range rangeUs,
                                                          @NotNull IdeProfilerServices profilerService); static List<AllocationsInfo> getAllocationInfosForSession(@NotNull ProfilerClient client,
                                                                   @NotNull Common.Session session,
                                                                   @NotNull Range rangeUs,
                                                                   @NotNull IdeProfilerServices profilerService); static void trackAllocations(@NotNull StudioProfilers profilers,
                                      @NotNull Common.Session session,
                                      boolean enable,
                                      @Nullable Consumer<Memory.TrackStatus> responseHandler);  }
@Test public void testSaveHeapDumpToFile() { Assume.assumeFalse("Unified pipeline import cannot yet be tested because of dependencies on TransportService.getInstance().", myUnifiedPipeline); long startTimeNs = 3; long endTimeNs = 8; HeapDumpInfo dumpInfo = HeapDumpInfo.newBuilder().setStartTime(startTimeNs).setEndTime(endTimeNs).build(); SnapshotBuilder snapshotBuilder = new SnapshotBuilder(2, 0, 0) .addReferences(1, 2) .addRoot(1); byte[] buffer = snapshotBuilder.getByteBuffer(); myTransportService.addFile(Long.toString(startTimeNs), ByteString.copyFrom(buffer)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); MemoryProfiler.saveHeapDumpToFile(myStudioProfiler.getClient(), ProfilersTestData.SESSION_DATA, dumpInfo, baos, myStudioProfiler.getIdeServices().getFeatureTracker()); assertArrayEquals(buffer, baos.toByteArray()); }