0

Hello I must store all the elements of this structure in memory for processing, I cannot just load chunks of it at a time I must have it all loaded at any point so I'm trying to fit it all in the memory somehow it runs out of memory even though I have about 20 GB of RAM and maybe 17 GB left.. I understand that Collisions have a internal limit of 2 GB so RAM doesn't matter in this sense at all.

Here is how my layout looks like

public struct REGTYPE
{
    public byte REG_Kind; // ;1=8 bits \ 2=16 bits \ 3=32 bits \ 4=MMX \ 5=XMM \ 6=Float stack \ 7=Segment \ 8=Debug \ 9=Control \ 10=Test
    public byte REG_Ptr_Kind; // ;1=Byte PTR \ 2=Word PTR \ 3=Dword PTR \ 4=Qword PTR \ 5=mmword ptr \ 6=xmmword ptr \ 7=FWord PTR \ 8=tbyte ptr \ 9=null ptr (LEA)
    public byte REG_Type; //  ;0-7= direct register index \ 16 register=byte && 7 \ 32 register=(byte && 63)/8 \ 64=[32/16 address only] \ 128=[using x86 relatives]
    public byte REG_BaseAsReg; // ? ;1=Register only (BASE exposed)!
}

public struct REGSTRUCT
{
    public uint SEG_TYPE;
    public uint Base;
    public uint INDEX;
    public uint SCALE;
    public uint DISPLACEMENTS;
    public uint DISPLACEMENT_TYPE;
    public REGTYPE REG_Kind;
    public uint PTR_TYPE;
}

public struct IMMSTRUCT
{
    public uint VALUE_LO;
    public uint VALUE_HI;
    public uint VALUE_TYPE; //     1=Byte \ 2=Word \ 4=Dword \ 8=ByteToWord \ 16=ByteToDword \ 32=AbsJump \ 64=ShortJump \ 128=LongJump
}


public struct DisAsmStruct
{
    public uint Instruction_Prefix;
    public uint Instruction;
    public REGSTRUCT Reg1;
    public REGSTRUCT Reg2;
    public uint Reg_Reg; //1=from ptr
    public IMMSTRUCT Imm;
    public uint Instruction_Length;
}

public struct AsmStruct
{
    public uint Address;
    public string ASM;
    public DisAsmStruct disASM; //tried to add ref keyword no success
}

public static AsmStruct[] AsmCache = null;

In the middle of running this code it crashes on the Array.Resize line stating it ran out of memory when AshCache reaches about 2 million elements it has to go up to 10 maybe 20 million elements before it's done loading, I know it's possible to pack all this in structs without running out of memory as I did it in C++, I also believe I know the cause of this problem, the cause is that it stores the Structure as a copy of itself instead of just a reference to that struct, so twice as much memory is wasted for every struct how do I store a struct by reference in AsmCache? The reason I use Array.Resize is to increase the collection limit by 1000 everytime more instructions are found.. I don't just set to limit to some arbitrary limit. This is how I used to do it in Vb6 (ReDim Preserve)

DisAsmStruct DisA = new DisAsmStruct();
AsmCache = new AsmStruct[1001];
Array.Resize(ref AsmCache, AsmCache.GetUpperBound(0) + 1001);
AsmCache[Number + 1].Address = BaseAddress + CNT;
AsmCache[Number + 1].disASM = ref DisA; //this line will error as I tried to fix it (remove ref to work)
AsmCache[Number + 1].ASM = OpCode; //Like "MOV EAX, [ECX+34h]" blah blah
SSpoke
  • 5,656
  • 10
  • 72
  • 124
  • 1
    `In the middle of running this code it crashes` I don't see any code. Just definitions.... – Eser Jan 17 '16 at 21:32
  • 1
    You could compile your code as x64 and get larger virtual address space. This allows you to use more than 2G of RAM. – Yacoub Massad Jan 17 '16 at 21:33
  • I would change at least some of those structs into class, that way You would avoid creating duplicates. – Antonín Lejsek Jan 17 '16 at 21:39
  • @Eser I post the usage code on the bottom of the question sorry about that, but I just thought it's self-explaintory at this point just setting a string to .ASM, a struct to disASM, and a unsigned integer to Address and after a few million iterations I get a OutOfMemory crash – SSpoke Jan 17 '16 at 22:03

2 Answers2

1

If you want to allow objects that are larger than 2GB you will need to allow them. Below would go into your app.config file or another config file depending on your project type.

<configuration>
    <runtime>
        <gcAllowVeryLargeObjects enabled="true" />
    </runtime>
</configuration>

Documentation

https://msdn.microsoft.com/en-us/library/hh285054(v=vs.110).aspx

Matt Rowland
  • 4,575
  • 4
  • 25
  • 34
  • Wow this might be the answer thanks a bunch mate, Hey do you happen to know how to activate this key/value in Visual Studio 2015 IDE? nevermind I read documentation I see it's only like this – SSpoke Jan 17 '16 at 22:55
  • If you open the app.config file you can add that tag right in. Just make sure it is nested in the correct tags. – Matt Rowland Jan 17 '16 at 22:57
  • Ye, Looks like it did the trick, but same error occured at 1,720,000 elements. `An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll`. I'll try maybe changing it from AnyCPU to x64 or something like that. – SSpoke Jan 17 '16 at 23:01
  • Can't use x64 mode dammit doesn't load up 32 bit dlls. `Additional information: Unable to load DLL 'disASM.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)` – SSpoke Jan 17 '16 at 23:03
  • Did you try unchecking "Prefer 32-bit" flag in your project properties in "AnyCPU" mode? Is disASM.dll an x64 compatible library? – jamespconnor Jan 18 '16 at 00:08
  • @jamespconnor I wish disASM.dll was a x64 compatible library I would probably not have any more questions, but sadly nothing I can do it was built in 2003 for 32bit only no 64bit of this library exists.. sigh.. source code is said to be released in x86 assembly I wouldn't even know how to recompile it in 64bit even if I found it. Here is where I found it: http://web.vip.hr/inga.vip/test.htm it's the only library that I found is fast enough for my needs and provides excellent bindings/access of every bit of the instruction without any complications – SSpoke Jan 18 '16 at 09:18
1

Make AsmStruct a class, otherwise when You resize the array (or the List resizes itself) You copy all that data into new location (which is very slow btw). With AsmStruct as a class You copy just pointers. I made a simulation of Your resizing strategy and with class You can create 4x more items before OutOfMemory.

With editbin trick .NET Out Of Memory Exception - Used 1.3GB but have 16GB installed You can push the limit to 2x more.

If this is not enough and You have only 32bit library, You would have to split the program somehow. One 32bit program to communicate with library and one 64bit for processing.

Community
  • 1
  • 1
Antonín Lejsek
  • 6,003
  • 2
  • 16
  • 18
  • I'll check it out when I get home, started snowing out there! gonna go check it out, sounds like a solid fix though, 4x more items is probably enough. I will use a list if you say how it is.. to avoid using Resize copys – SSpoke Jan 18 '16 at 02:39