You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin中Array<Double>与DoubleArray的区别及JNI场景选型疑问

Core Differences Between Array<Double> and DoubleArray in Kotlin, Plus JNI Context

Great question—this is a common point of confusion, especially when working with JNI where the distinction hits close to home. Let’s break this down clearly:

Key Core Differences

  • Underlying Storage Type

    • val array: Array<Double> is an array of boxed Double objects. Each element is a full Java/Kotlin object stored on the heap, complete with an object header (metadata like hash code, GC flags, etc.).
    • val array: DoubleArray is an array of primitive double values. It maps directly to Java’s double[], which is a contiguous block of raw memory holding 8-byte floating-point numbers with no object overhead.
  • Memory Efficiency

    • Array<Double> wastes a lot of memory: each Double object takes ~24 bytes (16-byte object header + 8-byte double value) on most JVMs, compared to just 8 bytes per element in DoubleArray. For large arrays, this adds up drastically.
  • Nullability

    • Array<Double> allows null values (since it holds objects), which can be useful if you need to represent missing data.
    • DoubleArray cannot hold null—all elements default to 0.0, as primitive types don’t support nullability.

Performance: Is DoubleArray Worse?

Absolutely not—DoubleArray is far more performant than Array<Double>:

  • No boxing/unboxing overhead: Every time you read or write an element in Array<Double>, the JVM has to convert between the Double object and the primitive double (boxing when storing, unboxing when retrieving). DoubleArray skips this entirely.
  • Better cache locality: Since DoubleArray uses contiguous raw memory, CPU caches can fetch chunks of the array more efficiently, making loops and bulk operations much faster.

The only "disadvantage" of DoubleArray is the lack of null support—but that’s a deliberate tradeoff for performance and memory efficiency.

JNI Context: Why This Matters For You

Your JNI code highlights exactly why DoubleArray is the better choice here:

  • Working with Array<Double> (which maps to Java’s Double[]) requires you to manually create Double objects via constructors, as you noted with FindClass("java/lang/Double") and GetMethodID("<init>"). This adds significant overhead: each object creation involves JNI calls, heap allocation, and constructor execution.
  • DoubleArray maps directly to Java’s double[], which JNI supports natively. You can use functions like GetDoubleArrayElements() to get a direct pointer to the raw double values in memory, modify them directly, and then call ReleaseDoubleArrayElements() to sync back. This skips all the object-related hassle and is vastly more efficient.

So if you don’t need to store null values, always prefer DoubleArray—it’s faster, uses less memory, and makes your JNI code simpler and more performant.

内容的提问来源于stack exchange,提问作者Martin Perry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:07:53