Using Compress
is a good idea to save memory when working with large expressions. The problem is that Compress
ing large expressions also uses a large amount of memory to produce the compressed result (sometimes 10× the ByteCount
of the original expression). This can lead to unexpected kernel crashes. I tried MemoryConstrained
but it seems it does not catch Compress
's memory consumption.
The following example shows in steps how memory is consumed.
data = Range[30000000];
ByteCount[data] (* -> 120000104 *)
{MemoryInUse[], MaxMemoryUsed[]} (* -> {140468260, 140520204} *)
data = Compress[data];
ByteCount[data] (* -> 55320440 *)
{MemoryInUse[], MaxMemoryUsed[]} (* -> {75106708, 528489092} *)
As you can see, to compress 120MB of data the Compress
method uses additionally at least 380MB.
If you use larger data (for example use $10^8$ instead of $3\times10^7$), the kernel will probably ingloriously crash.
So:
I have seen that
Options[Compress] == {Method->{}}
. Is there a way to tweak the compression algorithm?Is there a way to "break" the expression in chunks (regardless the levels of the expression). My expression to compress may be a tree with "unbalanced" branches. Note that I tried to transform the expression to string (with
ToString
), but it also takes a huge amount of memory to do that.Using an external compression would be really nice, although this may lead to delays and cross-compatibility issues. Of course any ideas on that also would may prove usefull.
Any help will be really appreciate since I am puzzled with this the whole last week.