1

I have started programming in Go and I was wondering when new(Object) is used it allocates memory to the size of that object right? If this is the case how do I free this memory once I have finished using the object?

I ask this because in C++ when new is used on an object you can delete the object once there is no longer any need for the object to be stored.

I have been searching to see if Go does have delete or something similar to C++ but I have been unable to find anything.

Any help is much appreciated.

1
  • 1
    In Go memory-management should not be the responsibility of the developer, it will do the garbage collector. Commented Oct 24, 2016 at 10:47

3 Answers 3

4

As you see here:

Go is fully garbage-collected and provides fundamental support for concurrent execution and communication.

So you don't have to care about memory allocation.

Sign up to request clarification or add additional context in comments.

Comments

3

Go has garbage collection. This means the Go runtime checks in the background if an object or any other variable is not used anymore and if this is the case, frees the memory.

Also see the Go FAQ: Why is the syntax so different from C? - Why do garbage collection? Won't it be too expensive?

Comments

1

In Go, unlike in C and C++, but like in Java, memory is managed automatically by a garbage collector.

There is no delete to call.

Off-topic:

in C++ when new is used on an object you can delete the object once there is no longer any need for the object to be stored.

You must delete, otherwise you have memory leak.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.