Skip to content

Commit e37217c

Browse files
alekhyareddy28jjw24
authored andcommitted
From alekhyareddy28:memoryIssue on Jun 27, 2020
1 parent 3f0a766 commit e37217c

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

Flow.Launcher.Infrastructure/Image/ImageCache.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Threading.Tasks;
56
using System.Windows.Media;
67

7-
namespace Flow.Launcher.Infrastructure.Image
8+
namespace Wox.Infrastructure.Image
89
{
910
[Serializable]
1011
public class ImageCache
1112
{
12-
private const int MaxCached = 5000;
13+
private const int MaxCached = 50;
1314
public ConcurrentDictionary<string, int> Usage = new ConcurrentDictionary<string, int>();
1415
private readonly ConcurrentDictionary<string, ImageSource> _data = new ConcurrentDictionary<string, ImageSource>();
15-
16+
private const int permissibleFactor = 2;
1617

1718
public ImageSource this[string path]
1819
{
@@ -22,14 +23,39 @@ public ImageSource this[string path]
2223
var i = _data[path];
2324
return i;
2425
}
25-
set { _data[path] = value; }
26+
set
27+
{
28+
_data[path] = value;
29+
30+
// To prevent the dictionary from drastically increasing in size by caching images, the dictionary size is not allowed to grow more than the permissibleFactor * maxCached size
31+
// This is done so that we don't constantly perform this resizing operation and also maintain the image cache size at the same time
32+
if (_data.Count > permissibleFactor * MaxCached)
33+
{
34+
// This function resizes the Usage dictionary, taking the top 'maxCached' number of items and filtering the image icons that are not accessed frequently.
35+
Cleanup();
36+
37+
// To delete the images from the data dictionary based on the resizing of the Usage Dictionary.
38+
foreach (var key in _data.Keys)
39+
{
40+
int dictValue;
41+
if (!Usage.TryGetValue(key, out dictValue))
42+
{
43+
ImageSource imgSource;
44+
_data.TryRemove(key, out imgSource);
45+
}
46+
}
47+
}
48+
}
2649
}
2750

28-
public Dictionary<string, int> CleanupAndToDictionary()
29-
=> Usage
51+
public void Cleanup()
52+
{
53+
var images = Usage
3054
.OrderByDescending(o => o.Value)
3155
.Take(MaxCached)
3256
.ToDictionary(i => i.Key, i => i.Value);
57+
Usage = new ConcurrentDictionary<string, int>(images);
58+
}
3359

3460
public bool ContainsKey(string key)
3561
{
@@ -51,4 +77,4 @@ public int UniqueImagesInCache()
5177
}
5278
}
5379

54-
}
80+
}

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Flow.Launcher.Infrastructure.Logger;
1010
using Flow.Launcher.Infrastructure.Storage;
1111

12-
namespace Flow.Launcher.Infrastructure.Image
12+
namespace Wox.Infrastructure.Image
1313
{
1414
public static class ImageLoader
1515
{
@@ -218,27 +218,29 @@ public static ImageSource Load(string path, bool loadFullImage = false)
218218

219219
var img = imageResult.ImageSource;
220220
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
221-
{
222-
// we need to get image hash
223-
string hash = _enableHashImage ? _hashGenerator.GetHashFromImage(img) : null;
221+
{ // we need to get image hash
222+
string hash = EnableImageHash ? _hashGenerator.GetHashFromImage(img) : null;
224223
if (hash != null)
225224
{
226-
if (_guidToKey.TryGetValue(hash, out string key))
227-
{
228-
// image already exists
229-
img = _imageCache[key];
225+
int ImageCacheValue;
226+
if (GuidToKey.TryGetValue(hash, out string key))
227+
{ // image already exists
228+
if (ImageCache.Usage.TryGetValue(path, out ImageCacheValue))
229+
{
230+
img = ImageCache[key];
231+
}
230232
}
231233
else
232-
{
233-
// new guid
234-
_guidToKey[hash] = path;
234+
{ // new guid
235+
GuidToKey[hash] = path;
235236
}
236237
}
237238

238239
// update cache
239-
_imageCache[path] = img;
240+
ImageCache[path] = img;
240241
}
241242

243+
242244
return img;
243245
}
244246

0 commit comments

Comments
 (0)