r/ProgrammingLanguages • • 3d ago

Go with manual memory management / no garbage collector?

Go is my ideal programming language, except for there being no ergonomic way to work without the garbage collector (to my knowledge). Is there a language that has the simplicity and elegance of Go, but without a garbage collector?

Some big pros for Go in no particular order:

  • dead simple cross-compilation
  • first-class support for unit testing
  • quick builds
  • first-class support for debugging and profiling
  • goroutines and channels are extremely ergonomic primitives for concurrency

A con for Go:

  • the runtime is pretty hefty and results in large binaries.

Zig is the closest thing I have found, though I do find the build system a little complex. Profiling is do-able, though not great; the best way I've found is to use Tracy.

I am not a fan of Rust's syntax and am not able to develop at the speed of thought like I can with Go. This is definitely a skill issue but Rust just doesn't feel very ergonomic to me.

Odin is a strong contender. For profiling, Tracy seems to be the best option again, which isn't ideal. An allocator stored in context is exactly what I wish Go had. The syntax leaves something to be desired for me.

I need to look more into C3.

Any other languages I've missed?

24 Upvotes

50 comments sorted by

18

u/ProgrammingLanguager 3d ago

What are you looking for in a language, exactly? Why do you want full memory control?

Zig and Odin are, generally speaking, languages made for someone who has spent years writing C or C++. Zig, which is the language I have more experience with, is indeed simple and very flexible - much of its modernity derives straightforwardly from an intuitive extension of a const context to metaprogramming. Any language with significant metaprogramming faculties (especially, arbitrary compile time code execution) is going to get slow in larger projects utilizing them - although the tradeoff Zig takes for its implementation generally results in far better compilation times than rust's proc-macros. LLVM is a constant across both.

To make a wider point though - there is a reason the systems languages most often adopted by higher level programmers are often as complex as C++ and rust. Getting close to emulating the power of a GC (with its power to arbitrary derail execution at any point, etc) at compile time and/or with RTS-friendly dynamics is a hard problem! In some cases, it is better left to the user, but once your project gets large enough the burden of managing this will prevent you from "speed of thought" programming if you lack experience in the area (apologies if I'm making an undue assumption on that).

For simplicity and speed of thought writing, where you don't /need/ full manual memory management, languages like Nim or Swift, which borrow only the simplest of rust's tricks (move semantics and optimisations) and combine them with nicely abstracted reference counting via ARC, might be the better choice. You'll get good memory usage, tiny binaries, and predictable performance characteristics, while the source code can remain at a a level of abstraction and simplicity comparable to Go. This also applies to third party high performance/low footprint/embedded focused implementations of other, simple GC languages

21

u/SilvernClaws 3d ago edited 3d ago

Odin is pretty much the closest you'll get to Go syntax wise.

V is also pretty close in many aspects and has manual memory management in theory. Unfortunately the language is basically built around lying about how it manages memory and a bunch of other issues, so your mileage may vary.

C3 and Zig are both great options, but both not exactly similar to Go. It takes a while for Zig's build system to click, but then it gets much better. It's not declarative, you're basically building a graph of dependencies and steps programatically. Which other build tools also do, just not as explicitly.

11

u/vmcrash 3d ago

What exactly do you want to improve by removing the GC? Is Go too slow for your project? Do you notice some delays caused by the GC?

2

u/coderemover 3d ago

I don't know what the OP's problem is, but Go GC has some noticeable memory footprint. This is a common problem to all GC languages (Java is another one). It's very easy to beat Go programs on memory at least 3x with Rust.

2

u/pthierry 2d ago

I would expect that a generational GC can make the additional memory footprint pretty limited. In the computer language benchmark game, in fannkuch-redux, the smallest Rust program is bigger than the smallest Go program. In n-body, Go is only slightly bigger than Rust on that metric.

3

u/coderemover 2d ago

Those are constant memory benchmarks that do no heap allocations.

1

u/ProgrammingLanguager 3d ago

Memory is plentiful in most applications. The JVM is not designed for embedded devices, but for client devices and especially servers.The performance the large amount of memory used affords it is generally the right trade off. (this is coming from someone who, for experience reasons, would choose rust over java for basically all applications!)

0

u/coderemover 3d ago

That was 10 years ago. Today memory is expensive. The memory that otherwise is wasted by GC can be used for e.g. caching which improves performance.

3

u/ProgrammingLanguager 2d ago

You still have gigabytes on it on the device you mostly use for tasks a dumb terminal from the 80s could do fine. The cost of the JVM hasnt grown much (something that is even more the case for AOT runtimes), and while the current crisis in RAM is horrific if you’re trying to make a piece of hardware, it really doesn’t change the equation for a programming language.

The memory footprint of even quite beefy language runtimes doesn’t matter much (maybe until you get to the patently absurd like bundling a seperate, basically identical copy of some of the most complicated software out there with each app, I.e. electron style), absurd memory footprints are generally achieved by bad implementation of the app, the language used being more of a correlation with experience than a cause. This is because the runtime’s usage is generally less than linear in the memory usage of the app itself, so the best you’re gaining is making an app that doesn’t run on a system barely run there, lol (at least beyond trivial stuff, where the constant factor is very big in comparison to the app).

This applies less to languages like Go, but beefy runtimes and JITs have optimisation opportunities AOT compilers simply cannot replicate - their memory usage shouldn’t be viewed as a waste, they help these inherently more resource hungry languages catch up quite well.

-1

u/coderemover 2d ago edited 2d ago

This is irrelevant how much ram I have on my desktop/ laptop because desktop software is not being written in Java and Go (some are, but this is more an exception than a rule). On server side though, if I figured out a way to consume 3x less RAM I’d get a huge bonus.

And the JITs optimizations that AOT can’t do are just theoretical advantages that never materialized in practice. They are almost always offset by suboptimal design of Java language over-relying on runtime polymorphism and heap allocation. JITs cannot do expensive optimizations because they are constrained by time and memory. AOT can do whole program optimization and can spend 1000x more cpu cycles and memory. AOT can also use profiling data, it’s not limited to JITs, but the effectiveness of having the profile is usually very small - typically a few %.

3

u/vmcrash 2d ago

I would have expected to read

The memory that otherwise is wasted by GC can be used for e.g. running a local AI.

😁

5

u/potzko2552 3d ago

You are looking for Odin i think. But id give rust a try first

16

u/coderemover 3d ago edited 3d ago

The language you’re looking for is Rust, don’t judge it by the syntax. Syntax is a very subjective thing and don’t get fooled by the number of keywords. Using a dedicated keyword for e.g. controlling visibility is not any more complex than using capital / small letters or underscores.

14

u/vmcrash 3d ago

Using a dedicated keyword for controlling visibility is even better, because changing the visibility means to just perform one change at one place.

8

u/coderemover 3d ago

Exactly.

The OP also needs to bear in mind that GC actually brings a lot of ergonomy to languages like Java or Go, so they don't really want a Go with no GC. They really want a language where GC was replaced with something else that is at least trying to deliver similar level of experience. From all the languages I know of, Rust is the closest to that goal, and C++ is the second best.
But this comes with some complexity tax, there is no free lunch. The alternative is error prone manual memory management like C or Zig; but that is shifting complexity from the language to the developer.

9

u/moltonel 3d ago

Rust ticks a lot of OP's boxes, some better than Go, but it is not simple and does not build quickly. 

1

u/coderemover 3d ago edited 3d ago

It's as simple as one can get in a language without GC.

And many things allow the programs to be simpler and more ergonomic than Go - e.g. sum types (enums), derive macros, ? operator for error propagation, or RAII (you don't even need defer, things get closed automatically and it is much harder to forget).

As for building quickly - neither Go is as fast at building as it was 10 years ago, nor Rust is as slow as it was 10 years ago, and many of those Rust is slow to build are just repeated perceptions from v1.0. There are a few things in Rust you can do that can kill your build times (overusing macros or generics), but the equivalents don't exist in Go.

Some of my Rust projects which are 500k+ lines of code in 200+ libraries build in 10 seconds on my laptop, cold. That's quite impressive speed of building, comparable to other modern languages.

5

u/moltonel 3d ago

I love Rust, I generally choose it over Go, and I know where you're coming from, but

It's as simple as one can get in a language without GC.

is plainly false. Odin, Zig, C3, Nim, and many others are counter-examples. Rust might as simple as it gets considering all its goals, but there's a lot more to it than no GC and safety. Even within its comprehensive goals, Rust is now less simple than it could be. It worked fine before .async and ?, the macros systems would be designed very differently if they were added today, etc. Rust is very well designed, with all features justified and working in harmony, it's elegant and surprisingly easy to use. But it's still hard to recommend to somebody looking for a simple language.

Concerning build speed, yes the complaints are overblown, Rust has sped up while Go has slowed down, and devs should worry less about build speed (and about syntax, sigh). But there's still quite a gap between Go and Rust on that criteria, OP's argument is fair.

Some of my Rust projects which are 500k+ lines of code in 200+ libraries build in 10 seconds on my laptop, cold.

Lucky you. My 6K lines, 58 deps Rust project builds in 19s, about as long as my 1M line Go project. YMMV, don't trust anecdotes.

1

u/coderemover 3d ago

Zig moves the burden of memory management on the developer, like C. So it’s not comparable at all. Its simplicity comes from the fact of not having a GC replacement.

4

u/moltonel 3d ago

You absolutely can compare languages with different memory management strategies, many people do, especially on this sub. OP didn't ask for automatic memory management, they arguably did the opposite, and seem open to different feature compromises.

2

u/Key_Detail15 2d ago

Some of my Rust projects which are 500k+ lines of code in 200+ libraries build in 10 seconds on my laptop, cold. That's quite impressive speed of building, comparable to other modern languages.

So, about 50K lines/second? That sounds quite nippy for Rust. I first tested it in 2001 and one 1000-line test took 27 seconds.

I just downloaded Rustc to see what it's like now on my Windows PC. The following test compiles to a .obj file (needs MS/VS utilities otherwise).

This test is just repetitions of a += b+c*d; in one function (+= to stop it complaining about unused value of 'a'):

          N=10K     N=100K     N=1000K lines
Rustc     4 secs    40 secs**   --           ** fails at 40 secs
Rustc -O  1 sec     10 secs     220 secs

The first line gives 2.5Klps speed. The second gives 10Klps (but slows down for the 1Mloc test), however nearly all the code is elided: that 1Mloc test generates only a 3KB output! So it not doing all the work.

For comparison, on the same machine, my own everyday compiler (for my language) has a throughput of 500Klps, about 200 times faster, and it always does all the work.

Further, it generates approx 20 bytes of output per line; Rustc (when -O is not used so it produces the expected amount of output) generates 700 bytes per line.

For example, 7MB for N=10K, compared to 215KB. This might be one reason Rustc is slower.

2

u/coderemover 2d ago

You’re testing with a single thread. Real projects use multiple crates that are compiled in parallel.

3

u/Key_Detail15 2d ago

If you are comparing raw compilation speed then you need to use a single thread. Otherwise any build-stats are meaningless.

Once you have raw speed, THEN you can use parallel execution to speed things up. (I can run my compiler in parallel too!) You shouldn't use multiple cores INSTEAD of improving a too-slow compiler.

If that 50Klps per second thing was with multiple threads, then it is misleading.

However, none of this explains why that binary output was so large.

3

u/nzconstructionlawyer 3d ago

It is not. There are numerous examples of far simpler languages without GC.

The additional features you list add complexity to the language and to programs. They make snippets of code look simpler and therefore you think they make programs simpler but programs are not their source code; the map is not the territory.

RAII for example adds enormous semantic complexity to the language (all the complexity around Drop).

The ? operator needs to be unwrapped in your head into the same control flow but it's implicit.

Macros are extremely complex.

Rust compile times are atrocious. In practice they're far worse than they were a few years ago because the ecosystem has exploded in complexity.

2

u/mamcx 2d ago

Aside for:

am not able to develop at the speed of thought like I can with Go

This is very normal, and is the initial barrier feels very high. I use more than a dozen langs professionally and move to Rust from F#/Python and 3 months in I thinking I was a failure as developer.

But once you understand the trick to climb the barrier is easier and now I write rust much faster (overall) than even python!.

The main thing: Rust is composed of layers, and the #1 mistake is start doing a full stack web app that force to do all layers at once, that is killing for someone learning.

Rust has gradual complexity. Go to https://doc.rust-lang.org/stable/book/ and you will see that most of it look trivial. The only weird part is the thing about combining generics AND lifetimes (that is where the actual rust is hard lands!).

You can write simple rust easy: Structs, Enums, pattern matching, functions. Not add generic, lifetimes (or at least, not at first and not both at once), clone like hell (use Rc or Arc). And use the most standard traits: Default, From, Iterator, Clone, Copy, Eq, Ord, Hash.

Do the middle/backend/logic layer to learn the core language. That is also the best way to architect a project, you left at the edge the complex stuff like async.

Once you master the core language, is time to do the asynchronous/lifetime/generics, etc, lear how impl Default, From, Iterator, Clone, Copy, Eq, Old, Hash and with that the rest come easy. The only major hurdle is try to do the internals of async but honestly that is not required for most people.

2

u/coderemover 2d ago

I can see people coming from reference heavy languages struggle with Rust a lot. The kind of developer who creates a cyclic graph of object references in Java or Python will have a very bad time with Rust. And this is actually good.

0

u/nzconstructionlawyer 3d ago

Rust is absolutely nothing like Go. It has extremely bad compile times, it's massively overcomplicated and it has a bad culture and so a bad ecosystem of packages.

1

u/coderemover 3d ago edited 3d ago

Compiling 100k lines per second is not “extremely bad compile times”. Go will always win at loc/s metric though, because of all that boilerplate that inflates the number of lines for the same functionality.

Comparing complexity makes no sense. Obviously an excavator is more complex than a shovel.

And you dare to speak about overcomplicating things when your language of choice did this atrocity:
https://mcyoung.xyz/2024/12/16/rangefuncs/
Seriously?

Every feature that Go steals from Rust turns out to be either limited (generics) or more complex (iterators). Can’t wait until Go team steals enums (there are plans).

3

u/MCWizardYT 2d ago edited 2d ago

The rangefunc is actually pretty readable, reminds me of IEnumerable in C#

But also I'd like to mention that the Java example is purposefully verbose/contrived, and that you can do all of that in one line: Iterator<Integer> nonZeroIterator = Arrays.stream(array).filter(n -> n != 0).iterator();

(array here is a int[]. If you don't want to box the integers, you can replace Iterator<Integer> with PrimitiveIterator.OfInt and call nextInt() instead of next())

This article was written in 2024 so it was already possible to write it that way for like a whole decade....

0

u/coderemover 2d ago

The Java example is to illustrate that whenever you have to construct an iterator manually, it is not very ergonomic because of next/hasNext split. That's why all iterators in our Java code must be wrapped in AbstractIterator (from Guava). The original Java Iterator interface wasn't well thought out. Sure, now you have some nice builders to make iterators from streams, they are nice to write, but their performance is often quite bad, those things allocate lot of stuff on the heap. And Go has nothing like that.

1

u/garagedragon 2d ago edited 2d ago

Rust is more complicated than go because 1) it has to deal with manual memory management at all, but also 2) it offers more options for expressing and managing the complexity of your problem in the code, whereas Go just leaves you to it - it doesn't reduce the complexity of the problem, but it doesn't give you any tools for managing it.

As the bedrock example, I think the (val, err) form is just dire compared to Result/Either. Sure, you've achieved the basic task of giving me the normal return value or an error value, but you don't do anything to stop me from 1) using the wrong one on the caller side and just acting on the nil (which will not necessarily cause a crash...!) or 2) stop the callee accidentally producing invalid pairs where both or neither are non-nil.

You can say "skill issue" if you want, but I can't write code perfectly because nobody can, and I want my tools to help catch inevitable mistakes as soon as possible, that's what they're there for.

This sort of weirdness in design also compounds, because despite multiple return values being allowed, tuples are not, so I can't treat the (val, err) pair as a unit and chain/map further operations on it. I have to check for the err being nil inline, which means that if I have a bunch of fallible calls in a row, every single one right-shifts the actual work I want to do when everything goes right. I either end up with a ton of small, possibly repetitive functions, or code so deeply nested it runs off the monitor.

0

u/coderemover 2d ago

> 1) it has to deal with manual memory management at all

Manual memory management in Rust is optional.
It offers fully automatic and deterministic management through static scoping, move semantics and a bit of reference counting. This adds *some* learning curve, but once you learn it, you don't have to manually manage memory at all if you don't want to.

2) - agreed

2

u/garagedragon 2d ago

I mean "manual" as in, as opposed to a GC, not as opposed to RAII and all that good stuff. The programmer still has to pay attention to memory lifetimes, even though the normal case just works without having to do anything explicit except maybe implement Drop

1

u/Inconstant_Moo 🧿 Pipefish 2d ago

"Stole from Rust"? Graydon Hoare invented generics?

-1

u/nzconstructionlawyer 2d ago

Go isn't my language of choice.

Your comment is pathetic. Pretending Rust is some powerful superlanguage just makes you look like a complete programming beginner tbh. Languages largely don't matter and there's more relevant important software written in Go than in Rust. It's a very practical language.

-1

u/coderemover 2d ago

Languages do not matter the same way as it doesn’t matter what material you build your house of and with what tools. Good craftsmen use good professional grade equipment, not toys.

0

u/nzconstructionlawyer 2d ago

Yes and Rust is a toy for people more interested in thinking about programming than they are in actually programming.

0

u/coderemover 2d ago

Yet popularity of Rust grows extremely fast and Go not so much. And Rust is used in much more complex projects like operating system kernels, browsers, high scale networking infrastructure, database systems.

-1

u/5nord 3d ago

When the keyword is optional, the language increases your cognitive load. In C++ for example default is public for global symbols, but private for classes. Every language has slightly different defaults.

Go is very explicit and enforces discipline as a language design choice. Go has a very different philosophy than Rust.

2

u/slaymaker1907 2d ago

Don't forget that structs are also public by default and are otherwise pretty much the same as classes.

2

u/GardenerAether 2d ago

i remember thinking that memory management could be fully automated without the need for a garbage collector using a continuation passing style wrapper and linear types. james faure seems to agree with this so its nice to know im not completely crazy lmao. im not entirely sure of any existing implementations myself though, haha '

3

u/L8_4_Dinner (Ⓧ Ecstasy/XVM) 2d ago

Go is my ideal programming language, except for there being no ergonomic way to work without the garbage collector (to my knowledge). Is there a language that has the simplicity and elegance of Go, but without a garbage collector?

Blender is my ideal food blending tool, except for there being no ergonomic way to work without the noise of the motor (to my knowledge). Is there a kitchen tool that has the simplicity and elegance of a blender, but without any noise?

The issue here is you're not thinking of the design as a whole, e.g. why did Go include a garbage collector as part of its design?

The GC is not a "feature" of Go. It's an intrinsic building block. A lot of the things that are "simple" in Go can't exist without a GC.

If you have alternative ideas, try writing the code by hand in the Go-like language that you're thinking about, and then write by hand the (C/assembly/whatever) code that it will produce (or could produce) out of its compiler back end. Reason out to yourself how that produced code could have been produced from the inputs. Repeat the exercise until you're happy with the source code and with the back end output. Then write down the specs and build the compiler. The result will be so popular that you'll have to beat off the inevitable crazy fans with a stick.

Good luck!

3

u/tandycake 3d ago

Another choice would be Jai.

Odin and C3 are great options though.

However, all 3 of these are lacking in a good desktop GUI framework. If you're more into games, CLI apps, and/or immediate-mode GUIs, then no problem.

1

u/Glittering_Gur5701 3d ago

C with something like libtask

1

u/chibuku_chauya 2d ago

Yeah, you missed C.

1

u/matthieum 2d ago

It's not clear to me how low-level you wish to go. Your mention that being able to customize Allocator is a strong point for Odin doesn't match any of the points in the pros/cons on Go, for example.

It's also unclear if you're looking for a language to put code in production today -- Go & Rust are mature -- or you're working towards an ideal -- Odin & Zig are not mature.

It's also unclear how much memory safety matters to you. Rust is memory safe, Go nearly is, but Odin & Zig are definitely not.

If you're open to less mature implementations, which will affect the quality of the debugging experience, then you may be interested in exploring the "Rust alternatives". A lot of people have been working on Rust without borrow-checking, or Rust with more flexible borrow-checking, and there may be something there that tickles your fancy.

For Rust without borrow-checking, Hylo offers an interesting paradigm I think. No more lifetimes/groups at all! Woot!

Otherwise, on the Rust with more flexible borrow-checking, and even less mature than Hylo, you may want to check Valen (which aims at excellent Rust FFI) or Zeta (which was announced ~2 months ago).

1

u/five5years 1d ago

Weird that you say Odin's syntax "leaves something to be desired". It is about as close to Go syntax and your described tastes as possible.

1

u/ProgrammingLanguager 1d ago

To give a very straightforward answer as opposed to my long comment: Hare (harelang.org) is in a lot of ways exactly what you've described

1

u/kanareyka 3d ago

Hare might fit