mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Added cache clear button, added cache size estimation
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user