import resource

import pytest

from .models import TestModel as DirtyMixinModel

pytestmark = pytest.mark.django_db


def test_rss_usage():
    DirtyMixinModel()
    rss_1 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    for _ in range(1000):
        DirtyMixinModel()
    rss_2 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    assert rss_2 == rss_1, 'There is a memory leak!'

import gc
import resource

import pytest

from .models import TestModel as DirtyMixinModel

pytestmark = pytest.mark.django_db


def test_rss_usage():
    DirtyMixinModel()
    gc.collect()
    rss_1 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    for _ in range(1000):
        DirtyMixinModel()
    gc.collect()
    rss_2 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    assert rss_2 == rss_1, 'There is a memory leak!'

Call gc.collect() before measuring memory usage.
Call gc.collect() before measuring memory usage.

+ import gc
  import resource
  
  import pytest
  
  from .models import TestModel as DirtyMixinModel
  
  pytestmark = pytest.mark.django_db
  
  
  def test_rss_usage():
      DirtyMixinModel()
+     gc.collect()
      rss_1 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
      for _ in range(1000):
          DirtyMixinModel()
+     gc.collect()
      rss_2 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
      assert rss_2 == rss_1, 'There is a memory leak!'
  
Call gc.collect() before measuring memory usage.
## Code Before:
import resource

import pytest

from .models import TestModel as DirtyMixinModel

pytestmark = pytest.mark.django_db


def test_rss_usage():
    DirtyMixinModel()
    rss_1 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    for _ in range(1000):
        DirtyMixinModel()
    rss_2 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    assert rss_2 == rss_1, 'There is a memory leak!'

## Instruction:
Call gc.collect() before measuring memory usage.
## Code After:
import gc
import resource

import pytest

from .models import TestModel as DirtyMixinModel

pytestmark = pytest.mark.django_db


def test_rss_usage():
    DirtyMixinModel()
    gc.collect()
    rss_1 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    for _ in range(1000):
        DirtyMixinModel()
    gc.collect()
    rss_2 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    assert rss_2 == rss_1, 'There is a memory leak!'
