Hi,
So i did some work on it.
I run it now locally with recreated data in mssql 300k rows 67 columns and debugg set to False, so its powershell with
python manage.py runserver
To summarize:
- gc.collect() did not help after del every unnecessary variable,
- tracemalloc shows top 10 stats and differences but those values are so small compared to memory consumption:
[ Top 10 differences ]
...\venv\lib\site-packages\ldap3\protocol\rfc4512.py:94: size=584 B (-5151 KiB), count=1 (-69524), average=584 B
...\venv\lib\site-packages\ldap3\protocol\formatters\formatters.py:39: size=0 B (-2357 KiB), count=0 (-5607)
...\venv\lib\site-packages\ldap3\utils\asn1.py:149: size=34 B (-2270 KiB), count=1 (-5616), average=34 B
...\venv\lib\site-packages\ldap3\utils\ciDict.py:52: size=0 B (-1581 KiB), count=0 (-21952)
...\venv\lib\site-packages\ldap3\protocol\rfc4512.py:396: size=0 B (-1242 KiB), count=0 (-10965)
...\venv\lib\site-packages\ldap3\utils\ciDict.py:159: size=0 B (-480 KiB), count=0 (-5487)
...\venv\lib\site-packages\ldap3\protocol\rfc4512.py:86: size=440 B (-471 KiB), count=1 (-10968), average=440 B
...\venv\lib\site-packages\ldap3\protocol\rfc4512.py:467: size=0 B (-409 KiB), count=0 (-5484)
...\venv\lib\site-packages\ldap3\protocol\rfc4512.py:84: size=0 B (-381 KiB), count=0 (-5481)
...\venv\lib\site-packages\ldap3\protocol\rfc4512.py:538: size=0 B (-317 KiB), count=0 (-4188)
[ Top 10 ]
frozen importlib._bootstrap_external: 647: size=1825 KiB, count=19860, average=94 B
...devtools\Python3\lib\pathlib.py:198: size=1156 KiB, count=19621, average=60 B
...devtools\Python3\lib\pathlib.py:1232: size=273 KiB, count=2351, average=119 B
...devtools\Python3\lib\tracemalloc.py:505: size=222 KiB, count=4048, average=56 B
...devtools\Python3\lib\pathlib.py:728: size=201 KiB, count=1566, average=131 B
...devtools\Python3\lib\tracemalloc.py:498: size=190 KiB, count=4047, average=48 B
...devtools\Python3\lib\tracemalloc.py:193: size=190 KiB, count=4047, average=48 B
...devtools\Python3\lib\pathlib.py:79: size=147 KiB, count=1280, average=118 B
...devtools\Python3\lib\pathlib.py:706: size=142 KiB, count=1512, average=96 B
...devtools\Python3\lib\tracemalloc.py:67: size=110 KiB, count=1764, average=64 B
I thought maybe the view itself is badly designed and this is the issue (probably it is to be honest ) so i changed it to class based:
class HomeView(LoginRequiredMixin,ListView):
model=model
template_name='home.html'
context_object_name='data'
paginate_by= 25 # i usually run it with 100 or 500/page and memory usage is bigger then
def get_queryset(self):
codes = self.request.session.get('codes',[])
subquery=model.objects.filter(id=OuterRef('id')).values('id').annotate(max_date=Max('date')).values('max_date')
filtered_values.append(('code__in',codes))
filtered_values.append(('date',Subquery(subquery)))
filter_form=FilterForm(self.request.GET,codes=codes)
if filter_form.is_valid():
filtered_id_value=filter_form.cleaned_data['id']
if filtered_id_value:
filtered_values.append(('id',filtered_id_value))
filtered_code_value=filter_form.cleaned_data['codes_choices']
if filtered_code_value:
filtered_values.append(('code',filtered_code_value))
queryset=model.objects.filter(**dict(filtered_values))
self.request.session['filtered_values']=filtered_values
else:
filter_form=FilterForm(codes=codes)
return queryset.order_by('id')
def get_context_data(self, **kwargs):
column_names=[]
fields=model._meta.get_fields()
for field in fields:
column_names.append(field.name)
context['column_names']=column_names
codes = self.request.session.get('codes',[])
context['filter_form']=FilterForm(self.request.GET,codes=codes)
return context
I was also trying to observe how the memory acts while walking thru the pages. And even when i go back and forth memory keep increasing. So i thought maybe i should load the data with JSON instead of django queryset and it didnt help. So maybe its the issue with my HTML?
<form method="get" action="{% url 'home' %}">
{{filter_form.as_p}}
<input type="submit" value="Filter">
</form>
<div id="table-data" class="table-data">
<table id="content-table" class="scrollable-table">
<thead>
<tr>
<td><strong class="uppercase-text">Action</td>
{% for column_name in column_names %}
<td><strong class="uppercase-text">{{ column_name }}</strong></td>
{% endfor %}
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>
<a href="{% url 'edit' item.id item.snapshot %}">Edit</a>
</td>
{% for column in column_names %} <!-- i dont do it this way in my file but there are so many columns that i put it here in the nested loop so you can see what im doing hopefully -->
<td> {{ item.column }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Every loaded page on it adds 5-10 Mib with 25items/page for 100/page 25-35Mib for 500/page its 150-200 Mib in every loaded page. So html is also super simple and i dont know how to improve it.
Maybe im missing some config settings? But i don’t know which ones