On Tue, Oct 29, 2013 at 2:08 PM, Eric Z <
hadoo...@gmail.com> wrote:
>
> I am trying to grow a pretty large slice which takes more than half of the
> memory. Is there anyway I can do it easily without running into virtual
> memory?
>
> When I read the Go Slices: usage and internals, it says "To increase the
> capacity of a slice one MUST create a new, larger slice and copy the
> contents of the original slice into it." However, in my case, the slice is
> so large that if I create another one for copy it will definitely end up in
> spinning disks for virtual memory. Any workaround to add a small capacity to
> a large slice?
No. A slice's backing array must be in contiguous memory, so there is
no way to grow it beyond its capacity without copying it.
If I had a slice that large, I would consider another data structure,
one that does not require contiguous memory. Perhaps a simple slice
of slices. Indexing into it would be less efficient, but appending to
it would be faster.
> Another related question, can I manually free a slice memory space without
> waiting for the garbage collector?
You can't free a slice, but you can call runtime.GC().
Ian