Retrieval: Fix Memory Leak in Retrieval Query Handling #8955
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
I have read the contributing guidelines
Self-reported review complexity:
Description
This pull request addresses a memory leak issue in the
retrieval.cppfile, specifically when continuously accepting query inputs. The problem arises from thellama_batchinitialization and clearing process.Problem
The
llama_batch_initfunction allocates memory on the heap for the batch. However, the current implementation usesllama_batch_clearto reset the batch size to0, which does not properly free the allocated heap memory. This results in a continuous increase in memory usage as the process runs.Solution
The solution involves ensuring that the allocated memory for
llama_batchis properly freed after each query is processed. This prevents the memory leak and stabilizes the memory usage of the process.Changes
Replaced
llama_batch_clearwithllama_batch_freeto ensure proper memory deallocation.