the .vtu file to be genenrated is so large while using python

the .vtu file is so big that out of my memory…
This is the first time I have used the vtk package. I can generate an object with 2 hexahedrons well, but when I try to generate an object with 164640 hexahedrons(566049), the program can not run successfully because of the memory limit. By the way, the first file was 40GB, and the program encountered an error. this procedure was also time-consuming. I thought it should be my Incorrect usage.
here is my code

import vtk
# define the dimensions of the grid
size_nx = int(57)
size_ny = int(61)
size_nz = int(50)
#define the grid
grid = vtk.vtkUnstructuredGrid()

values = vtk.vtkFloatArray()
points = vtk.vtkPoints()
#set the dimensions of the grid
# grid.SetDimensions(size_nx, size_ny, size_nz)

coor3d = open('coor3d.txt', 'r')
res = open('res.txt', 'r')


points.SetDataTypeToDouble()
for k in range(size_nx * size_ny * size_nz):
    lines = coor3d.readline().split(" ")
    x_coor = float(lines[0])
    y_coor = float(lines[2])
    z_coor = float(lines[4])
    points.InsertNextPoint(x_coor, y_coor, z_coor)
grid.SetPoints(points)

n_p = points.GetNumberOfPoints()
print(f"num of points {n_p}")


nx = size_nx-1
ny = size_ny-1
nz = size_nz-1

for i in range(nx* ny * nz):
    # poisition of the 8 vertices of the hexahedron
    v1 = int(i % nx)
    v2 = int((i / nx) % ny)
    v3 = int(i / (nx * ny))

    hex = vtk.vtkHexahedron()
    hex.GetPointIds().SetId(0, v1 + v2 * size_nx + v3 * size_nx * size_ny)
    hex.GetPointIds().SetId(1, v1 + v2 * size_nx + v3 * size_nx * size_ny + 1)
    hex.GetPointIds().SetId(2, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx + 1)
    hex.GetPointIds().SetId(3, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx)
    hex.GetPointIds().SetId(4, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx * size_ny)
    hex.GetPointIds().SetId(5, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx * size_ny + 1)
    hex.GetPointIds().SetId(6, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx * size_ny + size_nx + 1)
    hex.GetPointIds().SetId(7, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx * size_ny + size_nx)
    if range(0,nx* ny * nz-1,30500).count(i):
        print(f"{i*100/(nx* ny * nz):.2f}%")
    grid.InsertNextCell(hex.GetCellType(), hex.GetPointIds())
    value = res.readline().split("\n")  # 从
    values.InsertNextValue(float(value[0]))
    grid.GetCellData().AddArray(values)
    
res.close()
coor3d.close()    
# save the grid as a vtu file
writer = vtk.vtkXMLUnstructuredGridWriter()
#compressor = vtk.vtkZLibDataCompressor()
writer.SetFileName("output_res.vtu") # set the file name
writer.SetInputData(grid) # set the grid as input
#writer.SetCompressor(compressor)
writer.Write() # write the file

I can not attach coor3d and res files. if you need them, I can upload them on my GitHub.