# Too much allocations

**URL:** <https://discourse.julialang.org/t/too-much-allocations/57366>\
**Category:** Performance\
**Created:** [March 17, 2021, 9:14am UTC](https://discourse.julialang.org/t/too-much-allocations/57366 "2021-03-17T09:14:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![powerzf](https://avatars.discourse-cdn.com/v4/letter/p/8e8cbc/32.png) [@powerzf](https://discourse.julialang.org/u/powerzf)\
**Post date:** [March 17, 2021, 9:14am UTC](https://discourse.julialang.org/t/too-much-allocations/57366/1 "2021-03-17T09:14:04Z")

</div>

```julia
abstract type CVGSurface end

struct Triangle <: CVGSurface
    triangleID :: Int64
    x :: Float64
    y :: Float64
    z :: Float64
end
Triangle() = Triangle(1,1.0,1.0,1.0)
    

struct TrBoundry <:CVGSurface
    triangleID1 :: Int64
    triangleID2 :: Int64
    triangleID3 :: Int64
    boundryID ::Int64
end
TrBoundry() = TrBoundry(1,1,1,1)
    

struct Surface <: CVGSurface
    n_max_vertex :: Int64
    n_vertices :: Int64
    n_triangles :: Int64
    triangles :: Array{Triangle,1}
    surfaces :: Array{TrBoundry,1}
end

function cvgseek2(file)
        filestream = open(file)
    
        fileline = readline(filestream)
        
        n_max_vertex, n_vertices, n_triangles = parse.(Int,split(fileline))
    
        triangles = Vector{Triangle}(undef,n_vertices)
        trBoundry = Vector{TrBoundry}(undef,n_triangles)
    
        for i in 1:n_vertices
            pos = position(filestream)
            seek(filestream, pos)
            string_triangleID, string_xyz... = cvg_split(filestream)
            triangleID = parse(Int,string_triangleID)
            x, y, z = parse.(Float64,string_xyz)
            triangles[i] = Triangle(triangleID, x, y, z)
        end
    
        for i in 1:n_triangles
            pos = position(filestream)
            seek(filestream, pos)
            triangleID1, triangleID2, triangleID3, boundryID = parse.(Int, cvg_split(filestream))
            trBoundry[i] = TrBoundry(triangleID1, triangleID2, triangleID3, boundryID)
        end
    
        return Surface(n_max_vertex, n_vertices, n_triangles, triangles, trBoundry)
     
    end

```

“Files” like this

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/0/103dde1883f751a07819bfc14bef40515d60e557.png)  
julia\> @btime cvgseek2(files)  
123.623 ms (144035 allocations: 15.89 MiB)

How to reduce allocation？

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 17, 2021, 9:19am UTC](https://discourse.julialang.org/t/too-much-allocations/57366/2 "2021-03-17T09:19:17Z")

</div>

Did you read [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/) yet? It has some good tips.

Also, you can use `@time` to see what lines in your code that allocate and figure out which ones of them are unnecessary.

You also haven’t provided all the functions needed to run the code and only an image of the input file.

I would try to reduce the code to a much smaller example and see if you can figure out what allocates. Dumping a big function into discourse and asking for someone to “clean it up” for you tends to not work very well.

Also, the

```julia
pos = position(filestream)
seek(filestream, pos)

```

looks like they don’t do anything, or?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 17, 2021, 10:29am UTC](https://discourse.julialang.org/t/too-much-allocations/57366/3 "2021-03-17T10:29:31Z")

</div>

Besides the performance tips pointed out by @kristoffer.carlsson , consider reusing other projects that already have a lot of the functionality you seem to be building. For example, we are working intensively everyday in Meshes.jl to provide a good set of mesh types for use across Julia ecosystems: [https://github.com/JuliaGeometry/Meshes.jl](https://github.com/JuliaGeometry/Meshes.jl)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 17, 2021, 12:06pm UTC](https://discourse.julialang.org/t/too-much-allocations/57366/4 "2021-03-17T12:06:58Z")

</div>

Here are some notes to help tracking the allocations: [Tracking allocations · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/memory/)

---

<div class="post-metadata">

**Author:** ![powerzf](https://avatars.discourse-cdn.com/v4/letter/p/8e8cbc/32.png) [@powerzf](https://discourse.julialang.org/u/powerzf)\
**Post date:** [March 18, 2021, 12:33am UTC](https://discourse.julialang.org/t/too-much-allocations/57366/5 "2021-03-18T00:33:59Z")

</div>

[https://github.com/powerzf/CVGSurfaceFile](https://github.com/powerzf/CVGSurfaceFile)

This is the url。
