Creating a DataTexture3D with data consumes double memory

I am measuring the memory consumption when creating a DataTexture3D.

If I create the texture first with a size and empty data, the memory consumption equals to the size of the texture as expected:

var tex = new THREE.DataTexture3D(null, 512, 512, 400);

I can later update the texture using gl.texSubImage3D, but this is a bit inconvenient.

However, if I try to feed actual data into the constructor, the memory consumption is double:

var data = new Uint16Array(512 * 512 * 400);
var tex = new THREE.DataTexture3D(data, 512, 512, 400);

It seems like Three.JS retains a copy of my data array. Can this be prevented? Any ideas?

I’m curious how you’re measuring the memory allocation closely enough to know it’s exactly doubled?

My guess would be that you’re seeing a copy of the memory in GPU memory and another in main memory. three.js holds a copy of most resources (mesh geometry, texture data) in memory in case they need to be updated on the GPU. I believe it’s possible to remove the in-memory copy if you’re sure you won’t need to update the GPU memory later, but this is a bit hard to be sure of (e.g. to handle GL context loss).

If there’s more memory allocation beyond main memory + GPU memory then I’m not sure, it’s not impossible that there could be a bug here.

I’m measuring with performance.memory.usedJSHeapSize. It’s not super accurate, but the results are very consistent. It’s consuming ~200 MB extra with 512x512x400 Uint16 voxels. The figures correspond well to having an extra copy of the input data.

Been a few issues in the past with memory. Check your version and make sure its not an old R87 version. Also check out this stuff. Not sure how much applies to your ThreeJS version but could be applicable.

Thank you – I had a fairly old version. Updating to a newer version seems to make a significant difference.

1 Like

I replied a bit too quickly there… it turns out that the update did not solve the problem.

Perhaps I can make my own Data3DTexture which deletes the extra copy after it has been uploaded to the GPU.