Skip to content
39 changes: 38 additions & 1 deletion src/LogExpert.Core/Classes/Log/LogBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,20 @@ public void AddLine (LogLine lineMemory, long filePos)

public void ClearLines ()
{
Array.Clear(_lineArray, 0, LineCount);
if (_lineArray == null)
{
_lineArray = ArrayPool<LogLine>.Shared.Rent(MAX_LINES);
_lineArrayLength = _lineArray.Length;
}
else
{
Array.Clear(_lineArray, 0, LineCount);
}

LineCount = 0;
#if DEBUG
_filePositions.Clear();
#endif
}

/// <summary>
Expand All @@ -129,6 +141,31 @@ public void Reinitialise (ILogFileInfo fileInfo, int maxLines)
#endif
}

/// <summary>
/// Evicts the buffer content to free memory while preserving metadata (LineCount, StartLine, StartPos, Size).
/// The buffer remains findable in buffer list lookups and can be re-read from disk when accessed.
/// </summary>
public void EvictContent ()
{
if (_lineArray != null)
{
Array.Clear(_lineArray, 0, LineCount);
ArrayPool<LogLine>.Shared.Return(_lineArray);
_lineArray = null;
}

// Do NOT zero LineCount — it is needed for buffer lookup in GetBufferForLineWithIndex.
// Do NOT zero StartLine, StartPos, Size — they are needed for re-reading from disk.
IsDisposed = true;
#if DEBUG
DisposeCount++;
#endif
}

/// <summary>
/// Fully disposes the buffer content and resets all metadata. Used when the buffer is being returned to the pool
/// or completely removed from the buffer list.
/// </summary>
public void DisposeContent ()
{
if (_lineArray != null)
Expand Down
9 changes: 6 additions & 3 deletions src/LogExpert.Core/Classes/Log/LogfileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,13 @@ public async Task<ILogLineMemory> GetLogLineMemoryWithWait (int lineNum)
else
{
_isFastFailOnGetLogLine = true;
_logger.Debug(CultureInfo.InvariantCulture, "No result after {0}ms. Returning <null>.", WAIT_TIME);
_logger.Info(CultureInfo.InvariantCulture, "Entering fast-fail mode for line {0}. No result after {1}ms.", lineNum, WAIT_TIME);
}
}
}
else
{
_logger.Debug(CultureInfo.InvariantCulture, "Fast failing GetLogLine()");
_logger.Info(CultureInfo.InvariantCulture, "Fast-fail returning null for line {0}", lineNum);
if (!_isFailModeCheckCallPending)
{
_isFailModeCheckCallPending = true;
Expand Down Expand Up @@ -1590,7 +1590,10 @@ private void GarbageCollectLruCache ()
try
{
removed.LogBuffer.AcquireContentLock(ref lockTaken);
_bufferPool.Return(removed.LogBuffer);
// Evict content but preserve metadata (LineCount, StartLine, etc.)
// so the buffer remains findable in _bufferList lookups.
// Do NOT return to pool — the buffer is still referenced by _bufferList.
removed.LogBuffer.EvictContent();
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public class PositionAwareStreamReaderSystem : PositionAwareStreamReaderBase, IL

private int _newLineSequenceLength;

private string _currentLine; // Store current line for Memory<char> access

public override bool IsDisposed { get; protected set; }

#endregion
Expand Down Expand Up @@ -85,7 +83,6 @@ public bool TryReadLine (out ReadOnlyMemory<char> lineMemory)
}

// Store line for Memory access
_currentLine = line;
lineMemory = line.AsMemory();
return true;
}
Expand All @@ -100,7 +97,6 @@ public bool TryReadLine (out ReadOnlyMemory<char> lineMemory)
public void ReturnMemory (ReadOnlyMemory<char> memory)
{
// No-op for System reader - string is already managed by GC
_currentLine = null;
}

#endregion
Expand Down
Loading
Loading