避免在 Java 中进行数组初始化
Avoid array initialization in Java
是否可以在 Java 中避免数组 zeroing/initialization?
我需要快速分配新的固定长度的字节数组,这些数组将完全填充预定义的数据。因此,我不需要 JVM 在实例化时自动将数组清零,而且我当然不介意数组是否包含垃圾。我所需要的只是恒定时间数组分配,不幸的是,由于提到的归零问题,它变成了 O(n)。
会使用不安全的帮助吗?
JVM 总是初始化数组。但是你可以重复使用同一个数组,它会被初始化一次。
class sun.misc.Unsafe
未正式记录。
来自http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/
Avoid initialization
allocateInstance method can be useful when you need to skip object
initialization phase or bypass security checks in constructor or you
want instance of that class but don't have any public constructor.
一些资源表明它已从 Java 9.
中删除
- 你有什么样的应用程序数组初始化成为性能瓶颈?
- 回应"Sadly I can't because these arrays are held on to for an undefined period of time by an asynchronous networking library. Reusing them results in overwriting partially unsent messages.":
然后使用池并仅重用当前未使用的数组。不过,您将不得不管理它。那么,数组创建 真的 是个大问题吗?
是否可以在 Java 中避免数组 zeroing/initialization?
我需要快速分配新的固定长度的字节数组,这些数组将完全填充预定义的数据。因此,我不需要 JVM 在实例化时自动将数组清零,而且我当然不介意数组是否包含垃圾。我所需要的只是恒定时间数组分配,不幸的是,由于提到的归零问题,它变成了 O(n)。
会使用不安全的帮助吗?
JVM 总是初始化数组。但是你可以重复使用同一个数组,它会被初始化一次。
class sun.misc.Unsafe
未正式记录。
来自http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/
Avoid initialization
allocateInstance method can be useful when you need to skip object initialization phase or bypass security checks in constructor or you want instance of that class but don't have any public constructor.
一些资源表明它已从 Java 9.
中删除- 你有什么样的应用程序数组初始化成为性能瓶颈?
- 回应"Sadly I can't because these arrays are held on to for an undefined period of time by an asynchronous networking library. Reusing them results in overwriting partially unsent messages.": 然后使用池并仅重用当前未使用的数组。不过,您将不得不管理它。那么,数组创建 真的 是个大问题吗?