Memory / Runtime / Allocator - Go vs Java

Memory management, pointers, and profiling are fundamental aspects of efficient code. Let's consider three key concepts: slice backing array, pointer, and profiling (pprof / trace), and compare Go with Java.

1. Slice backing array

A slice in Go is a lightweight structure over an array: it holds a pointer to the array, length, and capacity.


arr := [5]int{1,2,3,4,5}
s := arr[1:4]      // {2,3,4}, holds a reference to arr
s[0] = 20
fmt.Println(arr)   // [1,20,3,4,5]
  

Tip: if you change elements of a slice, remember that it changes the backing array and can unexpectedly affect other slices.

“A slice in Go is not just an array, but a tool for safe memory handling that allows sharing and extending data without copying”

The Java equivalent is ArrayList or an array, but copying a slice does not change the original:


int[] backingArray = new int[]{1,2,3,4,5};
int[] slice = Arrays.copyOfRange(backingArray, 1, 4);
slice[0] = 20;
System.out.println(Arrays.toString(backingArray)); // [1,2,3,4,5]
  

2. Pointer / References

Go allows using pointers directly:


x := 10
p := &x
*p = 20
fmt.Println(x) // 20
  

Tip: pointer arithmetic in Go is dangerous—use it only where absolutely necessary. In most cases, slices and structs are sufficient.

In Java, all objects are passed by reference, while primitives are passed by value. Example:


Integer x = 10;
Integer y = x; // copy of the reference
// Changing an immutable object is not possible, but array or List objects can be modified via reference
  

3. Profiling (pprof / trace)

Go has built-in tools for CPU and memory profiling:


import _ "net/http/pprof"
go http.ListenAndServe(":6060", nil)
  

Java uses JFR, VisualVM, Mission Control:


// java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -jar app.jar
  

Tip: always enable profiling during development or staging to see real memory and CPU issues.

4. Summary Comparison Table

Feature Go Java
Slice / ArrayList Slice holds a reference to the array, len, cap ArrayList holds an internal array, copies do not change the original
Pointer / Reference Can be modified directly References to objects, primitives by value
Profiling pprof / trace JFR, VisualVM, Mission Control
Memory safety Garbage collector + unsafe capabilities GC + type safety

5. Conclusion and Recommendations

  • For Go: pay close attention to slices and pointers, profile with pprof.
  • For Java: use references and built-in collections, profiling through JFR/VisualVM.
  • General principles: understanding runtime, allocator, and reference model helps write efficient code and avoid unexpected side effects.

“Knowing how slices, pointers, and garbage collection work is critical to building high-performance and safe applications. In Go and Java, the approaches are different, but the concept of memory management and profiling is the same”


🌐 На русском
Total Likes:0

Оставить комментарий

My social media channel
By sending an email, you agree to the terms of the privacy policy

Useful Articles:

Java v25: Choosing the Right Multithreading for Any Task
Introduction The Java world is rapidly evolving, and with each version, new tools are emerging for effectively working with multithreading, collections, and asynchrony. Java 25 brings powerful feature...
Let's look at: Trace, Profiling, Integration Testing, Code Coverage, Mocking, Deadlock Detection in Go vs Java | Testing, Debugging and Profiling
Series: Go for Java Developers — analysis of trace, profiling and testing In this article we will analyze tools and practices for testing, debugging and profiling in Go. For a Java developer this wil...
Java under the Microscope: Stack, Heap, and GC using Code Examples
Diagram - Java Memory Model - Heap / Non-Heap / Stack Heap (memory for objects) Creates objects via new. Young Generation: Eden + Survivor. Old Generation: objects that have survived several GC c...

New Articles:

Concurrency is not about “starting many threads”. It’s about agreements between them. Imagine a restaurant kitchen: — cooks (threads / goroutines) — orders (tasks) — and the main question: how do th...
When HashMap starts killing production: the engineering story of ConcurrentHashMap
Imagine a typical production service. 32 CPU hundreds of threads configuration / session / rate limits cache tens of thousands of operations per second And somewhere inside — a regular Map. At first...
Zero Allocation in Java: what it is and why it matters
Zero Allocation — is an approach to writing code in which no unnecessary objects are created in heap memory during runtime. The main idea: fewer objects → less GC → higher stability and performance. ...
Fullscreen image