Filling the memory

muslimah

Honorable
Dec 5, 2012
2
0
10,510
Hello,
i want my application to give no memory available error.So i have to fill the memory somehow..how do i do it?
just running a program which allocates a lot of memory will do the rick?
 

Sunius

Distinguished
Dec 19, 2010
390
0
19,060


That will not happen. Neither GCC, not Visual C++ does that.
 

randomizer

Distinguished
Are you sure? I know that the C# compiler certainly will for a similar piece of code when building with optimisations, although it will leave it if you don't enable optimisations. eg.

Code:
class Program
    {
        private static void Main(string[] args)
        {
            while (true)
            {
                int p = new int();
            }
        }
    }

Spits out this CIL without optimisations:

Code:
.method private hidebysig static 
	void Main (
		string[] args
	) cil managed 
{
	// Method begins at RVA 0x2050
	// Code size 11 (0xb)
	.maxstack 1
	.entrypoint
	.locals init (
		[0] int32 p,
		[1] bool CS$4$0000
	)

	IL_0000: nop
	IL_0001: br.s IL_0007
	// loop start (head: IL_0007)
		IL_0003: nop
		IL_0004: ldc.i4.0
		IL_0005: stloc.0
		IL_0006: nop

		IL_0007: ldc.i4.1
		IL_0008: stloc.1
		IL_0009: br.s IL_0003
	// end loop
} // end of method Program::Main

And with optimisations:

Code:
.method private hidebysig static 
	void Main (
		string[] args
	) cil managed 
{
	// Method begins at RVA 0x2050
	// Code size 2 (0x2)
	.maxstack 8
	.entrypoint

	// loop start
		IL_0000: br.s IL_0000
	// end loop
} // end of method Program::Main

Obviously the compilers are different, but I'd imagine that those two that you mentioned would do something similar providing you enable the right optimisations.

@OP: Can't you just throw the exception manually? :lol:
 

Sunius

Distinguished
Dec 19, 2010
390
0
19,060
C# works differently than C++. C# that code wouldn't make sense due to garbage collector. In C#, you allocate and deallocate, which in the end doesn't change anything. In C++, all you do is allocate more and more. The heap keeps growing and it's changing.

In C++, however, there's no such thing.

Code compiled with full release optimizations:

Code:
void main()
{
	while (true)
	{
		int* p = new int;
	}
}

Code:
000E1002  xor		eax,offset __imp_operator new (0E20A0h) 
000E1007  push		4 
000E1009  call		esi 
000E100B  add		esp,4 
000E100E  jmp		main+7 (0E1007h)

The first line is initializing function main.

The second line allocates 4 bytes for int* p pointer variable.

The third line calls "operator new".

The fourth line deallocates 4 bytes of the pointer, since its scope expires.

And the final line commands the program to return to line 2.

That's it. It loops until it fills the memory and crashes.
 

Ijack

Distinguished
I've tested your code in VS and you are correct; the compiler doesn't optimize it.

There is a snag though. The assembler code that you list indicates that you compiled this as a 32-bit program. So at most it can only use 2GB of memory. So the heap only grows as far as 2GB and then the program crashes. Compile it as a 64-bit program and it will use as much memory as anyone here is likely to have.
 

Sunius

Distinguished
Dec 19, 2010
390
0
19,060
Gonna have to try it again. Tried compiling as 64 bit, however, it woulld still crash at 2 GB. Up until now, I used a .bat file to launch itself and the program. That way any PC would crash within 2 secs :).
 

melikepie

Distinguished
Dec 14, 2011
17
0
18,560

C# would just dump the memory every time the loop starts.
 

muslimah

Honorable
Dec 5, 2012
2
0
10,510
When i try the above method,my system hangs and wen i allocate a calculated value of memory, my application waits till the memory is available and does its work without throwing any out of memory error.
What i want to do is- since evry process will be allocated a part of memory from os,my application wil also have its set of memory.I want to access its memory from an other process so that wen my application tries to further allocate its memory,there s no memory available and it throws the error.