Filling the memory

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?
12 answers Last reply
More about filling memory
  1. This does the trick if you're using C/C++:

    while (true)
    {
        int* p = new int;
    }


    Make sure your PC doesn't hang, though!
  2. I wouldn't expect that program to use any memory. Any decent compiler will realize that the statement has no effect and will optimize it to an empty loop.
  3. Ijack said:
    I wouldn't expect that program to use any memory. Any decent compiler will realize that the statement has no effect and will optimize it to an empty loop.


    That will not happen. Neither GCC, not Visual C++ does that.
  4. 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.

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


    Spits out this CIL without optimisations:

    
    .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:

    
    .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:
  5. 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:

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


    
    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.
  6. 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.
  7. 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 :).
  8. Most compilers I've used allow you to turn optimizations on or off.
  9. randomizer said:
    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.

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


    Spits out this CIL without optimisations:

    
    .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:

    
    .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:

    C# would just dump the memory every time the loop starts.
  10. 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.
  11. That's not how it works. If you're out of memory, your system WILL HANG, instead of giving the error.
  12. With one extra line of code (really a change to an existing line of code) you could deliberately make it leak allocated memory each loop.
Ask a new question

Read More

Programming Memory Apps