-
-
Notifications
You must be signed in to change notification settings - Fork 435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collect memory usage in transactions #2445
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7195171
added IMemoryCollector, with different implementations for Java and A…
stefanosiano a70ace8
updated changelog
stefanosiano f14d9e8
get/set MemoryCollector methods marked as @Internal
stefanosiano d284d41
added a @Nullable annotation
stefanosiano 47c6487
Merge branch 'main' into feat/memory-usage-collection
stefanosiano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
sentry-android-core/src/main/java/io/sentry/android/core/AndroidMemoryCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.sentry.android.core; | ||
|
||
import android.os.Debug; | ||
import io.sentry.IMemoryCollector; | ||
import io.sentry.MemoryCollectionData; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.Internal | ||
public class AndroidMemoryCollector implements IMemoryCollector { | ||
@Override | ||
public MemoryCollectionData collect() { | ||
long now = System.currentTimeMillis(); | ||
long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); | ||
long usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize(); | ||
return new MemoryCollectionData(now, usedMemory, usedNativeMemory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
sentry-android-core/src/test/java/io/sentry/android/core/AndroidMemoryCollectorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.sentry.android.core | ||
|
||
import android.os.Debug | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotEquals | ||
import kotlin.test.assertNotNull | ||
|
||
class AndroidMemoryCollectorTest { | ||
|
||
private val fixture = Fixture() | ||
|
||
private class Fixture { | ||
val runtime: Runtime = Runtime.getRuntime() | ||
val collector = AndroidMemoryCollector() | ||
} | ||
|
||
@Test | ||
fun `when collect, both native and heap memory are collected`() { | ||
val usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize() | ||
val usedMemory = fixture.runtime.totalMemory() - fixture.runtime.freeMemory() | ||
val data = fixture.collector.collect() | ||
assertNotNull(data) | ||
assertNotEquals(-1, data.usedNativeMemory) | ||
assertEquals(usedNativeMemory, data.usedNativeMemory) | ||
assertEquals(usedMemory, data.usedHeapMemory) | ||
assertNotEquals(0, data.timestamp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.sentry; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** Used for collecting data about memory load when a transaction is active. */ | ||
@ApiStatus.Internal | ||
public interface IMemoryCollector { | ||
/** Used for collecting data about memory load when a transaction is active. */ | ||
@Nullable | ||
MemoryCollectionData collect(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.sentry; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@ApiStatus.Internal | ||
public final class JavaMemoryCollector implements IMemoryCollector { | ||
|
||
private final @NotNull Runtime runtime = Runtime.getRuntime(); | ||
|
||
@Override | ||
public @Nullable MemoryCollectionData collect() { | ||
final long now = System.currentTimeMillis(); | ||
final long usedMemory = runtime.totalMemory() - runtime.freeMemory(); | ||
return new MemoryCollectionData(now, usedMemory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.sentry; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.Internal | ||
public final class MemoryCollectionData { | ||
final long timestamp; | ||
final long usedHeapMemory; | ||
final long usedNativeMemory; | ||
|
||
public MemoryCollectionData( | ||
final long timestamp, final long usedHeapMemory, final long usedNativeMemory) { | ||
this.timestamp = timestamp; | ||
this.usedHeapMemory = usedHeapMemory; | ||
this.usedNativeMemory = usedNativeMemory; | ||
} | ||
|
||
public MemoryCollectionData(final long timestamp, final long usedHeapMemory) { | ||
this(timestamp, usedHeapMemory, -1); | ||
} | ||
|
||
public long getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public long getUsedHeapMemory() { | ||
return usedHeapMemory; | ||
} | ||
|
||
public long getUsedNativeMemory() { | ||
return usedNativeMemory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.sentry; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@ApiStatus.Internal | ||
public final class NoOpMemoryCollector implements IMemoryCollector { | ||
|
||
private static final NoOpMemoryCollector instance = new NoOpMemoryCollector(); | ||
|
||
public static NoOpMemoryCollector getInstance() { | ||
return instance; | ||
} | ||
|
||
private NoOpMemoryCollector() {} | ||
|
||
@Override | ||
public @Nullable MemoryCollectionData collect() { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
System.currentTimeMillis()
good enough as timestamp? Or should I go withDateUtils.getCurrentDateTime().getTime()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DateUtils
is usingCalendar
under the hood which itself seems to be usingSystem.currentTimeMillis()
. Thus I'd stick toSystem.currentTimeMillis()
.