I have tested with mesh.UploadMeshData(true)
profiler Screenshot by Lightshot show 78MB GfxDriver (“meshes: 56MB” in line also, near 17MB Gfx before mesh creation)
and 121MB used… as I understand this really not in JS unity HEAP which is 128MB allocated by me.
Also I have tested on 96MB heap in build, and it have not crashes.
(show used like 120-123MB, but I have allocated 96MB)
But it have crashes when 64MB heap 
and crashes when 80MB heap 
As I see in profiler, my peak memory like 24MB in mono + 22MB in unity, may be some peak mesh generation and uploading to GPU, but it must be 64-22-24=18MB free. No one of my meshes requires 18MB or more, it some MB each.
More, if 80 MB heap, it must be 80-22-24 = 34 MB free.
May be UploadMeshData don’t upload immediate and wait something sync signal? Or wait end of frame?
If it really waits something, my meshes must be stored in unity HEAP, and then uploaded to GPU, which increases crash risk.
My current code like this, in ONE FRAME make all of meshes
createMesh()
{
mesh = new Mesh()
assign pos, uv
mesh.UploadMeshData(true)
}
for (..)
{
createMesh()
}
Unity 5.6.5p4 (64-bit)
**
Edit: Also I have checked my code, and detected what my reusable arrays for mesh creation is copied to unity side totally.
MaxVerticesCount = 65535;
private static readonly Vector3[] ReusableVertices = new Vector3[MaxVerticesCount];
private static readonly Color32[] ReusableColors32 = new Color32[MaxVerticesCount];
private static readonly Vector2[] ReusableUv = new Vector2[MaxVerticesCount];
private static readonly Vector2[] ReusableUv2 = new Vector2[MaxVerticesCount];
createMesh()
{
// fill ReusableVertices , etc
mesh = new Mesh()
mesh.vertices = ReusableVertices;
mesh.colors32 = ReusableColors32;
// etc
mesh.UploadMeshData(true)
}
**
profiler
\ In Editor
when MaxVerticesCount = 65535 – then meshes 119MB Screenshot by Lightshot
when MaxVerticesCount = 32000 – then meshes 58MB Screenshot by Lightshot (2 times less)
\ In WebGL
when MaxVerticesCount = 65535 – then meshes 56 MB Screenshot by Lightshot
when MaxVerticesCount = 32000 – then meshes 29 MB Screenshot by Lightshot (2 times less)
Bad ways
- If I will insert “yield return null” for new frame after each mesh creation for UploadMeshData it will lead to decreased performance (many frames - many seconds), and reusable arrays will not give effect for speed (but GC will be ok)
- But if I will create new Vector3[ ] instead of ReusableVertices – it also will create garbage and increase memory consumption. And as webGL don’t have GC between frames, garbage will increase and lead to crash. Or must insert “yield return null” for new frame after each mesh creation too.
May be better ways
- insert some “yield return null”, but not for each mesh, for Upload pipeline, and use reusable arrays as now
- use reusable arrays with gradually size, for example: 64k, 32k, 16k - each will be selected for appropriate mesh creation (I have generate small and big meshes, and have vertices count for generation)
- use mesh.SetVertices() with Reusable List (not tested yet for GC alloc for list items, and for memory mesh total)
Can someone provide better solution?