在Java中,当内存损坏时,相同的引用/实例变量会发生什么?

huangapple go评论70阅读模式
英文:

What happens to same reference/instance variable in Java when memory gets corrupted?

问题

假设有两个变量指向同一个实例:

var obj1 = new Object();
var obj2 = obj1;

是否有可能因为内存位翻转(例如硬件故障)而导致 obj1 指向错误的内存地址?

因此,
obj1 指向无效的内存地址,
obj2 仍然有效,并指向被创建的 "new Object()"

英文:

Suppose there are two variables pointing to the same instance:

var obj1 = new Object();
var obj2 = obj1;

Is it possible that obj1 gets pointing to wrong memory address due to memory bit flip (e.g. hardware failures)?

so
obj1 points to invalid memory address
but obj2 is still valid, and points to "new Object()" created.

答案1

得分: 0

不适用于“Java语言”。语言规范假定机器正常工作,我假设你所说的“位翻转”是指硬件故障的一种类型。

如果你开始考虑诸如RAM的随机故障、宇宙射线、来自其他维度的全能存在等因素,那么一切皆有可能,但此时问题中应当删除“Java”标签。

在另一个回答下的一些评论之后更新:

我说“Java语言”,因为该问题将Java与JVM混淆。Java程序可以在多个虚拟机上运行,并且可以以不同的方式进行转译。Dalvik和JVM很常见(现在很难说哪个更常见),但仍然有很多代码使用两代GWT编译器、TeaVM、RoboVM。GraalVM变得越来越流行。

Java规范(也称为Java语言)不受物理世界的影响。它是一种语言的理想形式。问它是否会受到硬件故障的影响,就好像在问如果一个米是由劣质材料制成,是否可以比100厘米还要短。

另一方面,Java的现实世界实现会受到硬件故障的影响,但这些故障并不特定于Java。在大多数情况下,Java问题描述中的“变量”构造甚至不会与低级实现之间存在映射(可能会有一个堆栈、一个寄存器、闭包中的一个关键字,或者可能会被完全优化掉)。

因此,这个问题要么没有意义,要么与Java关系不大。

英文:

Not in "Java the language". The language specification assumes that the machine is working correctly, and I assume that by "bit flip" you mean the type of a hardware failure.

If you start to factor in things like random failures of RAM, cosmic rays, omnipotent beings from other dimensions etc. - then everything is possible, but then the "Java" tag should be removed from the question.

update (after some comments under another answer)

I said "Java the Language", because the question conflates Java with JVM. Java programs can run on multiple VMs - and can be transpiled in different ways. Dalvik and JVM are common (hard to say which one is more common nowadays), but there's still a lot of code using two generations of the GWT compiler, TeaVM, RoboVM. The GraalVM is becoming more and more popular.

Java specification (aka Java the Language) is not affected by the physical world. It is an ideal of a language. Asking if it could be affected by a hardware failure is like asking if a meter could be shorter than 100cm if it was made from a poor material.

On the other hand - the real-world implementations of Java are affected by hardware failures, but the failures are not Java-specific. In most cases there will not even exist a mapping between the Java construct of a "variable" as described in the question and the low-level implementation (there will be a stack, or a register, or a key in a closure, or it might get optimized away completely).

So, the question either doesn't make sense, or it has little to do with Java.

答案2

得分: -1

好的,以下是您要翻译的内容:

好的,让我们回答您的问题。

> 是否可能

是的。当然。

我们还要回答一些相当明显但未提及的后续问题:

> 这有多有可能?

非常不可能。

> Java 有没有任何内建的保护措施,比如内存页面的哈希值。

没有。

英文:

Well, let's just answer your question.

> Is it possible

Yes. of course.

Let's also answer some rather obvious, but unasked, followups:

> How likely is this?

Very unlikely.

> Does java have any built in protections, such as hashes of memory pages.

No.

huangapple
  • 本文由 发表于 2020年9月23日 20:50:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64028392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定