Skip to content

Commit

Permalink
Cache total memory on init and reuse later
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn committed Sep 13, 2023
1 parent 6240f81 commit da4c1b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public final class DeviceInfoUtil {
private final @Nullable ContextUtils.SideLoadedInfo sideLoadedInfo;
private final @NotNull OperatingSystem os;

private final @Nullable Long totalMem;

public DeviceInfoUtil(
final @NotNull Context context, final @NotNull SentryAndroidOptions options) {
this.context = context;
Expand All @@ -59,6 +61,13 @@ public DeviceInfoUtil(
isEmulator = buildInfoProvider.isEmulator();
sideLoadedInfo =
ContextUtils.retrieveSideLoadedInfo(context, options.getLogger(), buildInfoProvider);
final @Nullable ActivityManager.MemoryInfo memInfo =
ContextUtils.getMemInfo(context, options.getLogger());
if (memInfo != null) {
totalMem = getMemorySize(memInfo);
} else {
totalMem = null;
}
}

@NotNull
Expand Down Expand Up @@ -132,20 +141,11 @@ public Device collectDeviceInformation(
device.setProcessorCount(cpuFrequencies.size());
}

final @Nullable ActivityManager.MemoryInfo memInfo =
ContextUtils.getMemInfo(context, options.getLogger());
if (memInfo != null) {
// in bytes
device.setMemorySize(getMemorySize(memInfo));
if (collectDynamicData) {
device.setFreeMemory(memInfo.availMem);
device.setLowMemory(memInfo.lowMemory);
}
}
device.setMemorySize(totalMem);

// setting such values require IO hence we don't run for transactions
if (collectDeviceIO && options.isCollectAdditionalContext()) {
setDeviceIO(device);
setDeviceIO(device, collectDynamicData);
}

return device;
Expand Down Expand Up @@ -182,7 +182,7 @@ public ContextUtils.SideLoadedInfo getSideLoadedInfo() {
return sideLoadedInfo;
}

private void setDeviceIO(final @NotNull Device device) {
private void setDeviceIO(final @NotNull Device device, final boolean includeDynamicData) {
final Intent batteryIntent = getBatteryIntent();
if (batteryIntent != null) {
device.setBatteryLevel(getBatteryLevel(batteryIntent));
Expand All @@ -203,6 +203,14 @@ private void setDeviceIO(final @NotNull Device device) {
}
device.setOnline(connected);

final @Nullable ActivityManager.MemoryInfo memInfo =
ContextUtils.getMemInfo(context, options.getLogger());
if (memInfo != null && includeDynamicData) {
// in bytes
device.setFreeMemory(memInfo.availMem);
device.setLowMemory(memInfo.lowMemory);
}

// this way of getting the size of storage might be problematic for storages bigger than 2GB
// check the use of
// https://developer.android.com/reference/java/io/File.html#getFreeSpace%28%29
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DeviceInfoUtilTest {
}

@Test
fun `provides os and sideloaded info`() {
fun `provides os, memory and sideloaded info`() {
val deviceInfoUtil = DeviceInfoUtil.getInstance(context, SentryAndroidOptions())

val os = deviceInfoUtil.operatingSystem
Expand All @@ -48,6 +48,7 @@ class DeviceInfoUtilTest {
assertNotNull(sideLoadedInfo.isSideLoaded)

assertNotNull(deviceInfo.isSimulator)
assertNotNull(deviceInfo.memorySize)
}

@Test
Expand Down Expand Up @@ -98,6 +99,18 @@ class DeviceInfoUtilTest {
assertNotNull(deviceInfo.freeStorage)
}

@Test
fun `does not include device io data when disabled`() {
val options = SentryAndroidOptions().apply {
isCollectAdditionalContext = true
}
val deviceInfoUtil = DeviceInfoUtil.getInstance(context, options)
val deviceInfo = deviceInfoUtil.collectDeviceInformation(false, false)

assertNull(deviceInfo.storageSize)
assertNull(deviceInfo.freeStorage)
}

@Test
fun `does include dynamic data when enabled`() {
val options = SentryAndroidOptions().apply {
Expand Down

0 comments on commit da4c1b2

Please sign in to comment.