diff --git a/CHANGELOG.md b/CHANGELOG.md index 9773f3a15d..502b4a63b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixes +- Avoid concurrency in AndroidProfiler performance data collection ([#3130](https://github.com/getsentry/sentry-java/pull/3130)) - Improve thresholds for network changes breadcrumbs ([#3083](https://github.com/getsentry/sentry-java/pull/3083)) - SchedulerFactoryBeanCustomizer now runs first so user customization is not overridden ([#3095](https://github.com/getsentry/sentry-java/pull/3095)) - If you are setting global job listeners please also add `SentryJobListener` diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java index ee4dd84374..ff595b9191 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java @@ -313,26 +313,29 @@ private void putPerformanceCollectionDataInMeasurements( new ArrayDeque<>(performanceCollectionData.size()); final @NotNull ArrayDeque cpuUsageMeasurements = new ArrayDeque<>(performanceCollectionData.size()); - for (PerformanceCollectionData performanceData : performanceCollectionData) { - CpuCollectionData cpuData = performanceData.getCpuData(); - MemoryCollectionData memoryData = performanceData.getMemoryData(); - if (cpuData != null) { - cpuUsageMeasurements.add( - new ProfileMeasurementValue( - TimeUnit.MILLISECONDS.toNanos(cpuData.getTimestampMillis()) + timestampDiff, - cpuData.getCpuUsagePercentage())); - } - if (memoryData != null && memoryData.getUsedHeapMemory() > -1) { - memoryUsageMeasurements.add( - new ProfileMeasurementValue( - TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, - memoryData.getUsedHeapMemory())); - } - if (memoryData != null && memoryData.getUsedNativeMemory() > -1) { - nativeMemoryUsageMeasurements.add( - new ProfileMeasurementValue( - TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, - memoryData.getUsedNativeMemory())); + + synchronized (performanceCollectionData) { + for (PerformanceCollectionData performanceData : performanceCollectionData) { + CpuCollectionData cpuData = performanceData.getCpuData(); + MemoryCollectionData memoryData = performanceData.getMemoryData(); + if (cpuData != null) { + cpuUsageMeasurements.add( + new ProfileMeasurementValue( + TimeUnit.MILLISECONDS.toNanos(cpuData.getTimestampMillis()) + timestampDiff, + cpuData.getCpuUsagePercentage())); + } + if (memoryData != null && memoryData.getUsedHeapMemory() > -1) { + memoryUsageMeasurements.add( + new ProfileMeasurementValue( + TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, + memoryData.getUsedHeapMemory())); + } + if (memoryData != null && memoryData.getUsedNativeMemory() > -1) { + nativeMemoryUsageMeasurements.add( + new ProfileMeasurementValue( + TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, + memoryData.getUsedNativeMemory())); + } } } if (!cpuUsageMeasurements.isEmpty()) {