Search code examples
testinggroovymemory-leaksjmeterjsr223

Does vars.putObject and vars.getObject consume extra memory in jmeter


I have a test plan that looks like

Test plan
  Jsr223 sampler
  {
    def lst = [100 elements];
    vars.putObject("lst",lst);    
  }
  loop controller(100 times)
  {
    Http request
      preprocessor
      {
        lst = vars.getObject("lst");
      }
  }

Now does the lst in preprocessor uses the same memory of lst in jsr223 sampler or it creates its new memory and uses. Q2) Another question is, does the lst memory in preprocessor gets cleared for every iteration or does it created new memory for each iteration.


Solution

  • In your setup you always refer to the same object which lives in JMeterVariables class instance, it neither allocates new portion of memory nor frees it during new iterations.

    However be aware that each JMeter thread (virtual user) will have this object in its local storage so for 1 thread you will have 1 instance, for 2 threads - 2 instances.

    So if you have > 1 thread and use the same object across all threads - it's better to use props instead of vars, as per documentation:

    Properties are not the same as variables. Variables are local to a thread; properties are common to all threads

    If you want to clear the object manually use vars.remove() function where needed like:

    vars.remove('lst')
    

    In order to reduce memory consumption you might consider putting your objects into a CSV file and go for CSV Data Set Config which doesn't load the full file into memory and has flexible sharing options of the values across the threads.