LoginSignup
53
53

More than 5 years have passed since last update.

C# 製ソフトウェアで、メモリ不足になった場合の対処方法

Posted at

画像編集ソフト Paint.NET (4.0 Beta) で大きめのファイルを閲覧しようとしたところ

System.OutOfMemoryException: VirtualAlloc returned a null pointer, bytes = 13,362,240

というエラーが出て開けなかったのですが、その対処方法を備忘録として残しておきます。
今回は、設定を変更してメモリの割り当て量を無制限にすることで対応しました。
Eclispe のような Java 製ソフトウェアでメモリ不足のエラーが起こった時と同じ要領で対処可能みたいです。
その他の C# 製ソフトウェアについても応用できるはず。(たぶん)

1.設定ファイルを開く

Paint.NET の実行ファイルがあるフォルダに PaintDotNet.exe.config というファイルがあるので、テキストエディタで開きます。

2.メモリ割り当ての上限を無制限に設定

ファイルの中身がこんな感じになっているので

<?xml version="1.0"?>
<configuration>
  <runtime>
    <gcAllowVeryLargeObjects enabled="true" />
    <generatePublisherEvidence enabled="false" />
    <legacyCorruptedStateExceptionsPolicy enabled="true" />
    <loadFromRemoteSources enabled="true" />
  </runtime>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

runtime 要素の中に、以下の一行を挿入します。

    <nodeRunnerSettings memoryLimitMegabytes=”0”/>

最終的に以下の状態になれば OK です。

<?xml version="1.0"?>
<configuration>
  <runtime>
    <gcAllowVeryLargeObjects enabled="true" />
    <generatePublisherEvidence enabled="false" />
    <legacyCorruptedStateExceptionsPolicy enabled="true" />
    <loadFromRemoteSources enabled="true" />
    <nodeRunnerSettings memoryLimitMegabytes=”0”/>
  </runtime>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

これで今まで開けなかった巨大なファイルも読み込めるようになりました。
ただし、メモリを大量に消費して OS の挙動が不安定になるかもしれないので、その場合は memoryLimitMegabytes 属性を適切な値に調整する必要がありそうです。

53
53
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53
53