2023-05-26:golang关于垃圾回收和析构函数的选择题,多数人会选错。

news2024/10/5 21:18:22

2023-05-26:golang关于垃圾回收和析构的选择题,代码如下:

package main

import (
	"fmt"
	"runtime"
	"time"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

func main0() {
	a := &ListNode{Val: 1}
	b := &ListNode{Val: 2}
	runtime.SetFinalizer(a, func(obj *ListNode) {
		fmt.Printf("a被回收--")
	})
	runtime.SetFinalizer(b, func(obj *ListNode) {
		fmt.Printf("b被回收--")
	})
	a.Next = b
	b.Next = a
}

func main() {
	main0()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	fmt.Print("结束")
}

代码的运行结果是什么?并说明原因。注意析构是无序的。

A. 结束

B. a被回收–b被回收–结束

C. b被回收–a被回收–结束

D. B和C都有可能

答案2023-05-26:

golang的垃圾回收算法跟java一样,都是根可达算法。代码中main0函数里a和b是互相引用,但是a和b没有外部引用。因此a和b会被当成垃圾被回收掉。而析构函数的调用不是有序的,所以B和C都有可能,答案选D。让我们看看答案是什么,如下:

在这里插入图片描述

看运行结果,答案不是选D,而是选A。这肯定会出乎很多人意料,golang的垃圾回收算法是根可达算法难不成是假的,大家公认的八股文难道是错的?有这个疑问是好事,但不能全盘否定。让我们看看析构函数的源码吧。代码在 src/runtime/mfinal.go 中,如下:

// SetFinalizer sets the finalizer associated with obj to the provided
// finalizer function. When the garbage collector finds an unreachable block
// with an associated finalizer, it clears the association and runs
// finalizer(obj) in a separate goroutine. This makes obj reachable again,
// but now without an associated finalizer. Assuming that SetFinalizer
// is not called again, the next time the garbage collector sees
// that obj is unreachable, it will free obj.
//
// SetFinalizer(obj, nil) clears any finalizer associated with obj.
//
// The argument obj must be a pointer to an object allocated by calling
// new, by taking the address of a composite literal, or by taking the
// address of a local variable.
// The argument finalizer must be a function that takes a single argument
// to which obj's type can be assigned, and can have arbitrary ignored return
// values. If either of these is not true, SetFinalizer may abort the
// program.
//
// Finalizers are run in dependency order: if A points at B, both have
// finalizers, and they are otherwise unreachable, only the finalizer
// for A runs; once A is freed, the finalizer for B can run.
// If a cyclic structure includes a block with a finalizer, that
// cycle is not guaranteed to be garbage collected and the finalizer
// is not guaranteed to run, because there is no ordering that
// respects the dependencies.
//
// The finalizer is scheduled to run at some arbitrary time after the
// program can no longer reach the object to which obj points.
// There is no guarantee that finalizers will run before a program exits,
// so typically they are useful only for releasing non-memory resources
// associated with an object during a long-running program.
// For example, an os.File object could use a finalizer to close the
// associated operating system file descriptor when a program discards
// an os.File without calling Close, but it would be a mistake
// to depend on a finalizer to flush an in-memory I/O buffer such as a
// bufio.Writer, because the buffer would not be flushed at program exit.
//
// It is not guaranteed that a finalizer will run if the size of *obj is
// zero bytes, because it may share same address with other zero-size
// objects in memory. See https://go.dev/ref/spec#Size_and_alignment_guarantees.
//
// It is not guaranteed that a finalizer will run for objects allocated
// in initializers for package-level variables. Such objects may be
// linker-allocated, not heap-allocated.
//
// Note that because finalizers may execute arbitrarily far into the future
// after an object is no longer referenced, the runtime is allowed to perform
// a space-saving optimization that batches objects together in a single
// allocation slot. The finalizer for an unreferenced object in such an
// allocation may never run if it always exists in the same batch as a
// referenced object. Typically, this batching only happens for tiny
// (on the order of 16 bytes or less) and pointer-free objects.
//
// A finalizer may run as soon as an object becomes unreachable.
// In order to use finalizers correctly, the program must ensure that
// the object is reachable until it is no longer required.
// Objects stored in global variables, or that can be found by tracing
// pointers from a global variable, are reachable. For other objects,
// pass the object to a call of the KeepAlive function to mark the
// last point in the function where the object must be reachable.
//
// For example, if p points to a struct, such as os.File, that contains
// a file descriptor d, and p has a finalizer that closes that file
// descriptor, and if the last use of p in a function is a call to
// syscall.Write(p.d, buf, size), then p may be unreachable as soon as
// the program enters syscall.Write. The finalizer may run at that moment,
// closing p.d, causing syscall.Write to fail because it is writing to
// a closed file descriptor (or, worse, to an entirely different
// file descriptor opened by a different goroutine). To avoid this problem,
// call KeepAlive(p) after the call to syscall.Write.
//
// A single goroutine runs all finalizers for a program, sequentially.
// If a finalizer must run for a long time, it should do so by starting
// a new goroutine.
//
// In the terminology of the Go memory model, a call
// SetFinalizer(x, f) “synchronizes before” the finalization call f(x).
// However, there is no guarantee that KeepAlive(x) or any other use of x
// “synchronizes before” f(x), so in general a finalizer should use a mutex
// or other synchronization mechanism if it needs to access mutable state in x.
// For example, consider a finalizer that inspects a mutable field in x
// that is modified from time to time in the main program before x
// becomes unreachable and the finalizer is invoked.
// The modifications in the main program and the inspection in the finalizer
// need to use appropriate synchronization, such as mutexes or atomic updates,
// to avoid read-write races.
func SetFinalizer(obj any, finalizer any) {
	if debug.sbrk != 0 {
		// debug.sbrk never frees memory, so no finalizers run
		// (and we don't have the data structures to record them).
		return
	}
	e := efaceOf(&obj)
	etyp := e._type
	if etyp == nil {
		throw("runtime.SetFinalizer: first argument is nil")
	}
	if etyp.kind&kindMask != kindPtr {
		throw("runtime.SetFinalizer: first argument is " + etyp.string() + ", not pointer")
	}
	ot := (*ptrtype)(unsafe.Pointer(etyp))
	if ot.elem == nil {
		throw("nil elem type!")
	}

	if inUserArenaChunk(uintptr(e.data)) {
		// Arena-allocated objects are not eligible for finalizers.
		throw("runtime.SetFinalizer: first argument was allocated into an arena")
	}

	// find the containing object
	base, _, _ := findObject(uintptr(e.data), 0, 0)

	if base == 0 {
		// 0-length objects are okay.
		if e.data == unsafe.Pointer(&zerobase) {
			return
		}

		// Global initializers might be linker-allocated.
		//	var Foo = &Object{}
		//	func main() {
		//		runtime.SetFinalizer(Foo, nil)
		//	}
		// The relevant segments are: noptrdata, data, bss, noptrbss.
		// We cannot assume they are in any order or even contiguous,
		// due to external linking.
		for datap := &firstmoduledata; datap != nil; datap = datap.next {
			if datap.noptrdata <= uintptr(e.data) && uintptr(e.data) < datap.enoptrdata ||
				datap.data <= uintptr(e.data) && uintptr(e.data) < datap.edata ||
				datap.bss <= uintptr(e.data) && uintptr(e.data) < datap.ebss ||
				datap.noptrbss <= uintptr(e.data) && uintptr(e.data) < datap.enoptrbss {
				return
			}
		}
		throw("runtime.SetFinalizer: pointer not in allocated block")
	}

	if uintptr(e.data) != base {
		// As an implementation detail we allow to set finalizers for an inner byte
		// of an object if it could come from tiny alloc (see mallocgc for details).
		if ot.elem == nil || ot.elem.ptrdata != 0 || ot.elem.size >= maxTinySize {
			throw("runtime.SetFinalizer: pointer not at beginning of allocated block")
		}
	}

	f := efaceOf(&finalizer)
	ftyp := f._type
	if ftyp == nil {
		// switch to system stack and remove finalizer
		systemstack(func() {
			removefinalizer(e.data)
		})
		return
	}

	if ftyp.kind&kindMask != kindFunc {
		throw("runtime.SetFinalizer: second argument is " + ftyp.string() + ", not a function")
	}
	ft := (*functype)(unsafe.Pointer(ftyp))
	if ft.dotdotdot() {
		throw("runtime.SetFinalizer: cannot pass " + etyp.string() + " to finalizer " + ftyp.string() + " because dotdotdot")
	}
	if ft.inCount != 1 {
		throw("runtime.SetFinalizer: cannot pass " + etyp.string() + " to finalizer " + ftyp.string())
	}
	fint := ft.in()[0]
	switch {
	case fint == etyp:
		// ok - same type
		goto okarg
	case fint.kind&kindMask == kindPtr:
		if (fint.uncommon() == nil || etyp.uncommon() == nil) && (*ptrtype)(unsafe.Pointer(fint)).elem == ot.elem {
			// ok - not same type, but both pointers,
			// one or the other is unnamed, and same element type, so assignable.
			goto okarg
		}
	case fint.kind&kindMask == kindInterface:
		ityp := (*interfacetype)(unsafe.Pointer(fint))
		if len(ityp.mhdr) == 0 {
			// ok - satisfies empty interface
			goto okarg
		}
		if iface := assertE2I2(ityp, *efaceOf(&obj)); iface.tab != nil {
			goto okarg
		}
	}
	throw("runtime.SetFinalizer: cannot pass " + etyp.string() + " to finalizer " + ftyp.string())
okarg:
	// compute size needed for return parameters
	nret := uintptr(0)
	for _, t := range ft.out() {
		nret = alignUp(nret, uintptr(t.align)) + uintptr(t.size)
	}
	nret = alignUp(nret, goarch.PtrSize)

	// make sure we have a finalizer goroutine
	createfing()

	systemstack(func() {
		if !addfinalizer(e.data, (*funcval)(f.data), nret, fint, ot) {
			throw("runtime.SetFinalizer: finalizer already set")
		}
	})
}

看代码,看不出什么。其端倪在注释中。注意如下注释:

// Finalizers are run in dependency order: if A points at B, both have
// finalizers, and they are otherwise unreachable, only the finalizer
// for A runs; once A is freed, the finalizer for B can run.
// If a cyclic structure includes a block with a finalizer, that
// cycle is not guaranteed to be garbage collected and the finalizer
// is not guaranteed to run, because there is no ordering that
// respects the dependencies.

这段英文翻译成中文如下:

Finalizers(终结器)按照依赖顺序运行:如果 A 指向 B,两者都有终结器,并且它们除此之外不可达,则仅运行 A 的终结器;一旦 A 被释放,可以运行 B 的终结器。如果一个循环结构包含一个具有终结器的块,则该循环体不能保证被垃圾回收并且终结器不能保证运行,因为没有符合依赖关系的排序方式。

这意思很明显了,析构函数会检查当前对象A是否有外部对象指向当前对象A。如果有外部对象指向当前对象A时,A的析构是无法执行的;如果有外部对象指向当前对象A时,A的析构才能执行。

代码中的a和b是循环依赖,当析构判断a和b时,都会有外部对象指向a和b,析构函数无法执行。析构无法执行,内存也无法回收。因此答案选A。

去掉析构函数后,a和b肯定会被释放的。不用析构函数去证明,那如何证明呢?用以下代码就可以证明,代码如下:

package main

import (
	"fmt"
	"runtime"
	"time"
)

type ListNode struct {
	Val  [1024 * 1024]bool
	Next *ListNode
}

func printAlloc() {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	fmt.Printf("%d KB\n", m.Alloc/1024)
}

func main0() {
	printAlloc()
	a := &ListNode{Val: [1024 * 1024]bool{true}}
	b := &ListNode{Val: [1024 * 1024]bool{false}}

	a.Next = b
	b.Next = a

	// runtime.SetFinalizer(a, func(obj *ListNode) {
	// 	fmt.Printf("a被删除--")
	// })

	printAlloc()

}

func main() {
	fmt.Print("开始")
	main0()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	fmt.Print("结束")
	printAlloc()

}

在这里插入图片描述

根据运行结果,内存大小明显变小,说明a和b已经被回收了。

让我们再看看有析构函数的情况,运行结果是咋样的,如下:

package main

import (
	"fmt"
	"runtime"
	"time"
)

type ListNode struct {
	Val  [1024 * 1024]bool
	Next *ListNode
}

func printAlloc() {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	fmt.Printf("%d KB\n", m.Alloc/1024)
}

func main0() {
	printAlloc()
	a := &ListNode{Val: [1024 * 1024]bool{true}}
	b := &ListNode{Val: [1024 * 1024]bool{false}}

	a.Next = b
	b.Next = a

	runtime.SetFinalizer(a, func(obj *ListNode) {
		fmt.Printf("a被删除--")
	})

	printAlloc()

}

func main() {
	fmt.Print("开始")
	main0()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	time.Sleep(1 * time.Second)
	runtime.GC()
	fmt.Print("结束")
	printAlloc()

}

在这里插入图片描述

根据运行结果,有析构函数的情况下,a和b确实是无法被回收。

总结

1.不要怀疑八股文的正确性,golang的垃圾回收确实是根可达算法。

2.不要用析构函数去测试无用对象被回收的情况,上面的例子也看到了,两对象的循环引用,析构函数的测试结果就是错误的。只能根据内存变化,看无用对象是否被回收。

3.在写代码的时候,能手动设置引用为nil,最好手动设置,这样能更好的避免内存泄漏。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/573809.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

使用Jmeter进行http接口性能测试

在进行网页或应用程序后台接口开发时&#xff0c;一般要及时测试开发的接口能否正确接收和返回数据&#xff0c;对于单次测试&#xff0c;Postman插件是个不错的Http请求模拟工具。 但是Postman只能模拟单客户端的单次请求&#xff0c;而对于模拟多用户并发等性能测试&#xf…

Linux入门笔记

Linux 1.概述 2.命令 1.常用命令 新建文件 touch 新建目录 mkdir Linux命令格式⭐️ 选项可以对命令具体控制 2.文件目录操作 1.ls ll命令用的很多 显示出非隐藏文件的详细信息 2.cd ~表示当前用户目录&#xff0c;如root用户目录 \表示根目录 3.查看命令 1.cat查看命令 -n…

【Python爬虫项目实战四】Chatgpt国内接口分享第一期

目录 🍇前言接口一接口二(免费学习测试 wuguokai)接口三(AI文本工具站)🍇前言 前几次分享的py接口,由于不经常维护导致你们下载的时候已经失效了,为了回馈粉丝,昨晚我想实在不行我就弄个接口出来吧,我自己维护,有问题咱们就在评论下方留言,我及时做更新处理就可…

树形结构的表设计与Java接口实现

文章目录 1、开发需求中的树形结构2、表结构设计3、接口实现代码模型类与接口定义Mapper层开发 1、开发需求中的树形结构 树形结构在日常开发中很常见&#xff0c;如&#xff1a; 再比如&#xff1a; 还有&#xff1a; 2、表结构设计 这种树形结构&#xff0c;其 核心字段为p…

【自制C++深度学习推理框架】计算图的设计思路

计算图的设计思路 什么是计算图 在深度学习推理框架中&#xff0c;计算图是一种数据结构&#xff0c;它由算子节点和数据节点组成&#xff0c;在该图中前向传播时数据从输入节点开始流动&#xff0c;经过一层层的计算后输出到输出节点&#xff0c;表示深度学习模型的计算过程…

Unity Shader variants (shader 变体)

官方地址 https://docs.unity3d.com/cn/2022.2/Manual/SL-MultipleProgramVariants.html 教程可以看这里 https://www.jianshu.com/p/48ad75f0b4b9 https://www.jianshu.com/p/3e6b84317097 变种用我自己的理解就是 能用程序控制的shader 举个例子 这里声明了 a b c d 四个变…

数据结构与算法03:栈

目录 什么是栈&#xff1f; 栈在函数调用中的应用 栈的应用&#xff1a;如何实现浏览器的前进和后退功能&#xff1f; 每日一练&#xff1a;左右括号匹配 什么是栈&#xff1f; 简单地说&#xff0c;先进后出&#xff0c;后进先出的数据结构就是栈&#xff0c;可以理解为一…

面试题:什么是 TCP/IP?

目录标题 什么是 TCP/IP?1) 网络接口层:2) 网络层:3) 传输层:4) 应用层: 2.数据包3.网络接口层4.网络层1) IP:2)地址解析协议 ARP3)子网 5 传输层1&#xff09;UDP&#xff1a;2&#xff09;TCP&#xff1a; 6 应用层运行在TCP协议上的协议&#xff1a;运行在UDP协议上的协议&…

大模型即将改变世界,百度先上牌桌

“未来&#xff0c;所有的应用都将基于大模型来开发&#xff0c;每一个行业都应该有属于自己的大模型&#xff0c;大模型会深度融合到实体经济当中去。” 作者|思杭 斗斗 编辑|皮爷 出品|产业家 “大模型即将改变世界。”5月26日&#xff0c;李彦宏在中关村论坛说道。 而…

ESP32CAM开发板记录

忘记过去&#xff0c;超越自己 ❤️ 博客主页 单片机菜鸟哥&#xff0c;一个野生非专业硬件IOT爱好者 ❤️❤️ 本篇创建记录 2023-05-26 ❤️❤️ 本篇更新记录 2023-05-26 ❤️&#x1f389; 欢迎关注 &#x1f50e;点赞 &#x1f44d;收藏 ⭐️留言&#x1f4dd;&#x1f64…

【问题小记】解决Linux下php-fpm进程过多耗尽内存问题

最近一段时间&#xff0c;发现经常性的服务器内存耗尽&#xff0c;导致mysql服务down掉&#xff0c;一开始以为是mysql跑的太久占用较多内存&#xff0c;后来认真排查了一下原来是是PHP-FPM进程过多导致的。 今天一看内存又达到了82%&#xff0c;预计不会太久服务又会挂掉&…

深入探索: 对象构造的隐藏功能与技巧

&#x1f9d1;‍&#x1f4bb;CSDN主页&#xff1a;夏志121的主页 &#x1f4cb;专栏地址&#xff1a;Java基础进阶核心技术专栏 目录 &#x1f383; 一、重载 &#x1f384; 二、默认字段初始化 &#x1f386; 三、无参数的构造器 ✨ 四、显式字段初始化 &#x1f38a; 五…

Vue(路由插件)

一、介绍路由 1. 路由就是一组key-value的对关系&#xff0c;多个路由需要经过路由器进行管理 2. 主要应用在SPA&#xff08;单页面应用&#xff09; 在一个页面展示功能之间的跳转 特点&#xff1a; 当跳转时候不进行页面刷新路径随着变化展示区变化但是不开启新的页签 …

总结丨SGAT单基因关联分析工具,一文上手使用

SGAT是一个免费开源的单基因分析工具&#xff0c;基于Linux系统实现自动化批量处理&#xff0c;能够快速准确的完成单基因和表型的关联分析&#xff0c;只需要输入基因型和表型原始数据&#xff0c;即可计算出显著关联的SNP位点&#xff0c;并自动生成结果报告。 前段时间陆续的…

YOLOv5白皮书-第Y4周:common.py文件解读

目录 0.导入需要的包和基本配置1.基本组件1.1 autopad1.2 Conv1.3 Focus1.4 Bottleneck1.5 BottleneckCSP1.6 C31.7 SPP1.8 Concat1.9 Contract、Expand 2.重要类2.1 非极大值抑制&#xff08;NMS&#xff09;2.2 AutoShape2.3 Detections2.4 Classify &#x1f368; 本文为&am…

【头歌实训】【基于 Logisim 的 RISC-V 处理器设计 · 终】

真的恶心&#xff0c;我哭死 目录 前言 一、说明 1、参考 2、建议 二、处理器设计 三、Control器件设计 1、加速经常性事件&#xff0c;提高效率 2、控制信号设置 1.RegWEn 2.IMMSel 3.BSel 4.ALUSel & WBSel 5.MemWEn 6.PCSel & ASel 7.ALUB 总结…

【C语言】标准库(头文件、静态库、动态库),windows与Linux平台下的常用C语言标准库

一、Introduction1.1 C语言标准库1.2 历代C语言标准1.3 主流C语言编译器 二、C语言标准库2.1 常用标准头文件2.2 常用标准静态库 三、windows平台四、Linux平台五、常用头文件功能速览5.1 通用常用头文件01. stdio.h——标准输入输出02. stdlib.h——内存管理与分配、随机数、字…

Git常用命令reset和revert

Git常用命令reset和revert 1、reset 用于回退版本&#xff0c;可以指定退回某一次提交的版本。 checkout 可以撤销工作区的文件&#xff0c;reset 可以撤销工作区/暂存区的文件。 reset 和 checkout 可以作用于 commit 或者文件&#xff0c;revert 只能作用于 commit。 命…

为什么 String#equals 方法在做比较时没有使用 hashCode

一个疑问的引入 我之前出于优化常数项时间的考虑&#xff0c;想当然的认为 String#equals 会事先使用 hashCode 进行过滤 我想像中的算法是这样的 当两个 hashCode 不等时&#xff0c;直接返回 false&#xff08;对 hash 而言&#xff0c;相同的输入会得到相同的输出&#x…

数据安全复合治理框架和模型解读(0)

数据治理,数据安全治理行业在发展,在实践,所以很多东西是实践出来的,哪有什么神仙理论指导,即使有也是一家之说,但为了提高企业投产比,必要的认知是必须的,当前和未来更需要专业和创新。数据安全治理要充分考虑现实数据场景,强化业务安全与数据安全治理,统一来治理,…