Added cache clear button, added cache size estimation

This commit is contained in:
2025-11-20 22:02:28 +01:00
parent 116b47cdc0
commit baac2bce1d
5 changed files with 126 additions and 7 deletions

View File

@@ -53,4 +53,66 @@ public static class ImageHelper
Thread.Sleep(10);
}
}
public static long GetImageCacheSize()
{
long size = 0;
int stringOverhead = GetStringOverhead();
int arrayOverhead = GetArrayOverhead();
foreach (var kvOuter in ImageCache)
{
foreach (var kvInner in kvOuter.Value)
{
size += System.Text.ASCIIEncoding.Unicode.GetByteCount(kvInner.Key);
size += stringOverhead;
// byte[] size
if (kvInner.Value != null)
size += kvInner.Value.Length + arrayOverhead;
}
}
return size;
}
public static string ToHumanReadableSize(long bytes)
{
string[] sizes = ["B", "KB", "MB", "GB", "TB", "PB"];
double len = bytes;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len /= 1024;
}
return $"{len:0.##} {sizes[order]}";
}
private static int GetStringOverhead()
{
// Object header size
int header = IntPtr.Size == 8 ? 16 : 8; // 16 bytes on x64, 8 on x86
int size =
header +
sizeof(int) + // string length field
sizeof(char); // null terminator
return size;
}
private static int GetArrayOverhead()
{
// On x64 object header = 16 bytes; on x86 = 8 bytes
int header = IntPtr.Size == 8 ? 16 : 8;
// Array length field = 4 bytes
int lengthField = sizeof(int);
// Padding on x64 to align the elements (so the first element is aligned)
int padding = IntPtr.Size == 8 ? 4 : 0;
return header + lengthField + padding;
}
}