estimateSize static method
Estimates the size of an object in bytes.
This method provides a more accurate estimation of the memory footprint of various Dart types.
Implementation
static int estimateSize(dynamic value) {
if (value == null) {
return 0;
} else if (value is String) {
// UTF-16 encoding (2 bytes per character)
return value.length * 2;
} else if (value is num) {
// Numbers are typically 8 bytes (double) or 4 bytes (int)
return value is double ? 8 : 4;
} else if (value is bool) {
// Booleans are typically 1 byte
return 1;
} else if (value is DateTime) {
// DateTime is typically 8 bytes (64-bit timestamp)
return 8;
} else if (value is List) {
// For lists, estimate the size of each element plus overhead
int size = 16; // List overhead
for (var item in value) {
size += estimateSize(item);
}
return size;
} else if (value is Map) {
// For maps, estimate the size of each key-value pair plus overhead
int size = 32; // Map overhead
for (var entry in value.entries) {
size += estimateSize(entry.key) + estimateSize(entry.value);
}
return size;
} else if (value is Set) {
// For sets, estimate the size of each element plus overhead
int size = 16; // Set overhead
for (var item in value) {
size += estimateSize(item);
}
return size;
} else if (value is Uint8List || value is Int8List) {
// For byte arrays, each element is 1 byte
return value.length;
} else if (value is Uint16List || value is Int16List) {
// For 16-bit arrays, each element is 2 bytes
return value.length * 2;
} else if (value is Uint32List || value is Int32List || value is Float32List) {
// For 32-bit arrays, each element is 4 bytes
return value.length * 4;
} else if (value is Uint64List || value is Int64List || value is Float64List) {
// For 64-bit arrays, each element is 8 bytes
return value.length * 8;
} else {
// For other objects, serialize to JSON and measure
try {
final jsonString = jsonEncode(value);
return jsonString.length * 2;
} catch (e) {
// If serialization fails, use a rough estimate
return 100; // Arbitrary size for non-serializable objects
}
}
}