0

我有一个 C# 程序,需要使用 1GB 内存。但是我使用了一些需要额外 RAM 的适配器代码。看来我不能在运行时分配超过 400MB 的空间。

你能指出我正确的优化方向吗?

private static void FillHashTable()
{
  precomputed = new Hashtable( (Int32)( 50 * 1000000 ) );

  var htFileNames = Directory.EnumerateFiles( "precomputed_bin" );

  var input = new Byte[40000000];             // 40MB
  var decoded = new UInt32[input.Length/4];   // 40MB

  foreach ( var htFileName in htFileNames )
  {
    try 
    { 
      hStream = new FileStream( htFileName, FileMode.Open ); 
    }
    catch ( Exception e ) 
    {
      if ( hStream == null )
        return;

      hStream.Close();
      Console.WriteLine( e.ToString() ); 
      return; 
    }

    var br = new BinaryReader( hStream );
    input = br.ReadBytes( (int)hStream.Length );

    Buffer.BlockCopy( input, 0, decoded, 0, input.Length );

    foreach ( var n in decoded )
      precomputed.Add( n.GetHashCode(), n );        // 40MB per iter

    // close
    br.Close();
    br.Dispose();
    decoded = null;
    hStream.Close();
    hStream = null;
    GC.Collect();        
  }

  SerializeHashtable();
}

到目前为止,只能加载 40% 的数据,我需要将它们全部存储在 RAM 中。

我应该使用 C++ 来避免垃圾收集的不确定时间吗?(我现在的首选)

或者我应该将输入分成几块以重用它们并避免开销?

是我错误地释放资源还是 GC 失败了?

我应该以某种方式通知操作系统我需要超过 1GB 的空间吗?

谢谢!

4

1 回答 1

0

如果这是Java(你没有说),那么你可以通过指定-Xmx 参数来启动jvm。

此外,您可能会重新考虑您的应用程序是否真的需要一次将所有这些加载到内存中。

于 2013-05-17T21:20:54.563 回答