19

NDK のダウンロード ページには、「NDK の典型的な適切な候補は、信号処理や物理シミュレーションなど、多くのメモリを割り当てない自己完結型の CPU 集中型操作です」と記載されています。

私は C のバックグラウンドを持っており、NDK を使用して OpenGL ES 関数のほとんどと、物理学、頂点のアニメーションなどに関連するネイティブ関数を操作することに興奮していました。ネイティブコードを少し調べて、間違いを犯している可能性があるかどうか疑問に思っています。現時点ではテストに問題はありませんでしたが、将来問題が発生する可能性があるかどうかに興味があります.

たとえば、ゲーム構造体が定義されています (San-Angeles の例で見られるようなものです)。オブジェクトの頂点情報を動的にロードしているため (アクティブなゲーム領域に必要なものだけ)、頂点、法線、テクスチャ座標、インデックス、およびテクスチャ グラフィック データに対してかなりの量のメモリ割り当てが行われています。 . ゲーム領域間で割り当てられたものを解放することについて、私は非常に注意しています。

配列サイズにいくつかの上限を設定する方が安全でしょうか、それとも今行っているように勇敢に前進する必要がありますか?

4

1 に答える 1

10

NDK を使用するアプリケーションは、SDK を使用して開発されたアプリケーションと同様に動作する必要があるため、合理的なヒープ使用に関する最良のガイダンスは、 ActivityManager.javaのコメントから得られると思います。

/**
 * Return the approximate per-application memory class of the current
 * device.  This gives you an idea of how hard a memory limit you should
 * impose on your application to let the overall system work best.  The
 * returned value is in megabytes; the baseline Android memory class is
 * 16 (which happens to be the Java heap limit of those devices); some
 * device with more memory may return 24 or even higher numbers.
 */
public int getMemoryClass() {
    return staticGetMemoryClass();
}

/** @hide */
static public int staticGetMemoryClass() {
    // Really brain dead right now -- just take this from the configured
    // vm heap size, and assume it is in megabytes and thus ends with "m".
    String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m");
    return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
}

Dalvik VM のヒープ サイズを設定するコードはAndroidRuntime.cppにあり、 property_get関数を使用してネイティブ コードでヒープ割り当ての大まかな制限を決定する方法の例を提供します。

strcpy(heapsizeOptsBuf, "-Xmx");
property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
//LOGI("Heap size: %s", heapsizeOptsBuf);
opt.optionString = heapsizeOptsBuf;
mOptions.add(opt);

私が所有している 2 つの Android フォンのどちらにもデフォルトでプロパティが設定されていないため、デフォルト値16mはおそらく重要です。dalvik.vm.heapsize

于 2010-06-16T03:17:22.127 に答える