• Login
  • Register
  • Login Register
    Login
    Username/Email:
    Password:
    Or login with a social network below
  • Forum
  • Website
  • GitHub
  • Status
  • Translation
  • Features
  • Team
  • Rules
  • Help
  • Feeds
User Links
  • Login
  • Register
  • Login Register
    Login
    Username/Email:
    Password:
    Or login with a social network below

    Useful Links Forum Website GitHub Status Translation Features Team Rules Help Feeds
    Jellyfin Forum Development Server Development LibraryManager.cs Memory Cache

     
    • 0 Vote(s) - 0 Average

    LibraryManager.cs Memory Cache

    Excessive memory Usage due to lack of pruning
    TrueCodePoet
    Offline

    Junior Member

    Posts: 6
    Threads: 1
    Joined: 2025 Feb
    Reputation: 0
    #5
    2025-02-20, 03:24 PM
    As mentioned, I pulled the Source code down.  I am still running v 10.10 so this is the .Net 8 build.  The LibraryManager.cs code is basically the same.

    The subtle changes I have made are simple as a proof of concept.  Adding a TTL to the Cache in LibraryManager.

    here is my implementation : 

    Code:
            private const string ShortcutFileExtension = ".mblink";
            private static readonly TimeSpan DefaultTTL = TimeSpan.FromHours(2);
            private static readonly TimeSpan CleanupInterval = TimeSpan.FromHours(1);
            private readonly Timer _cleanupTimer;


    Added this to the constructor:

    Code:
                _cache = new ConcurrentDictionary<Guid, CacheEntry>();
                _cleanupTimer = new Timer(state => PurgeExpiredEntries(), null, CleanupInterval, CleanupInterval);


    Change the model being stored in Cach to :

    Code:
            private class CacheEntry
            {
                public CacheEntry(BaseItem item, DateTime expiration)
                {
                    Item = item;
                    Expiration = expiration;
                }
                public BaseItem Item { get; set; }
                public DateTime Expiration { get; set; }
            }


    Added a Cleanup to purge Expired entries :

    Code:
            private void PurgeExpiredEntries()
            {
                var purgeCount = 0;
                foreach (var kv in _cache)
                {
                    if (kv.Value.Expiration <= DateTime.UtcNow)
                    {
                        _cache.TryRemove(kv.Key, out _);
                        purgeCount++;
                    }
                }
                _lastCleanup = DateTime.UtcNow;
                _logger.LogInformation("Purged {Count} expired entries from the library cache", purgeCount);
                _logger.LogInformation("Library cache contains {Count} entries", _cache.Count);

            }



    An example use for get item : 

    Code:
            
    public BaseItem? GetItemById(Guid id)
            {
                if (id.IsEmpty())
                {
                    throw new ArgumentException("Guid can't be empty", nameof(id));
                }
                if (_cache.TryGetValue(id, out var entry))
                {
                    if (entry.Expiration <= DateTime.UtcNow)
                    {
                        _cache.TryRemove(id, out _);
                    }
                    else
                    {
                        if ((DateTime.UtcNow - _lastCleanup) > CleanupInterval)
                        {
                            PurgeExpiredEntries();
                        }
                        return entry.Item;
                    }
                }
                var item = RetrieveItem(id);
                if (item is not null)
                {
                    RegisterItem(item);
                }
                return item;
            }

    I have also added some general logging in areas I felt needed a little boost.

    So far my server is staying under 1 gig of ram.  but I am going to let it keep running.  I watch it bump when scans run and then settle back down a couple hours later.  I have noticed no real performance issues as I thought it could take a small hit.  But if you are using it.  then the cache is saturated.  If anything I think the overall performance is better especially on my resource starved backup server.

    I will keep you updated as we could have other areas that could use a bump.
    « Next Oldest | Next Newest »

    Users browsing this thread: 1 Guest(s)


    Messages In This Thread
    LibraryManager.cs Memory Cache - by TrueCodePoet - 2025-02-16, 05:06 AM
    RE: LibraryManager.cs Memory Cache - by TheDreadPirate - 2025-02-16, 06:34 PM
    RE: LibraryManager.cs Memory Cache - by TrueCodePoet - 2025-02-16, 10:12 PM
    RE: LibraryManager.cs Memory Cache - by TrueCodePoet - 2025-02-17, 03:03 PM
    RE: LibraryManager.cs Memory Cache - by TrueCodePoet - 2025-02-20, 03:24 PM
    RE: LibraryManager.cs Memory Cache - by TrueCodePoet - 2025-02-20, 03:33 PM
    RE: LibraryManager.cs Memory Cache - by TrueCodePoet - 2025-02-21, 12:58 PM

    • View a Printable Version
    • Subscribe to this thread
    Forum Jump:

    Home · Team · Help · Contact
    © Designed by D&D - Powered by MyBB
    L


    Jellyfin

    The Free Software Media System

    Linear Mode
    Threaded Mode