In the old days of Basic (starting over 4 decades ago), there were functions called Peek and Poke that would allow you to read and write memory directly. These were incredibly powerful commands: you could, for example, read and write directly to the hardware, like the video display, the tape cassette recorder, or the speaker. More modern versions of the language dropped Peek/Poke. However, you can still read and write memory, even from managed code, and this ability is still extremely powerful. You can’t write directly to the speaker, but you can do other fun things: see What is your computer doing with all that memory? Write your own memory browser and Use Named Pipes and Shared Memory for inter process communication with a child process or two The word “Managed” when applied to languages means that the memory that the program uses is automatically managed for the program: you can just use memory without paying attention to freeing it (usually). there is an automatic unused memory (“garbage”) collector.
(see Examine .Net Memory Leaks). In this sample we’ll create our own heap into which we’ll write and then read some memory. Keep in mind that the memory could come from anywhere, not just our own heap. We define a TestData structure that has only 2 integer data members. Strings are more complicated because of variable length, encoding, allocation issues.
(see HeapCreate and Marshal.PtrToStructure) Start Visual Studio 2010. (you can use older versions, but you’ll have to remove some of the new features I’ve used in the code)
File->New Project->(VB or C#) Windows ->WPF Application. Double click on the form designer to get the code behind file. Replace with the respective version from below. Make sure you have Tools->Options->Debugger->Just My Code unchecked. Put a breakpoint (F9) on the first line and single step through. Observe the values change as each line is executed. If you choose Debug->Windows->Memory1, you can try to see the memory by putting the Address ptr ( like 0x09C307D0) in the address window to see the memory. (In C#, you can drag/drop the address from the Locals window to the Memory window.) Right click on the memory window and choose to display the memory as 4 byte integers. Read more: Calvin Hsia's WebLog
(see Examine .Net Memory Leaks). In this sample we’ll create our own heap into which we’ll write and then read some memory. Keep in mind that the memory could come from anywhere, not just our own heap. We define a TestData structure that has only 2 integer data members. Strings are more complicated because of variable length, encoding, allocation issues.
(see HeapCreate and Marshal.PtrToStructure) Start Visual Studio 2010. (you can use older versions, but you’ll have to remove some of the new features I’ve used in the code)
File->New Project->(VB or C#) Windows ->WPF Application. Double click on the form designer to get the code behind file. Replace with the respective version from below. Make sure you have Tools->Options->Debugger->Just My Code unchecked. Put a breakpoint (F9) on the first line and single step through. Observe the values change as each line is executed. If you choose Debug->Windows->Memory1, you can try to see the memory by putting the Address ptr ( like 0x09C307D0) in the address window to see the memory. (In C#, you can drag/drop the address from the Locals window to the Memory window.) Right click on the memory window and choose to display the memory as 4 byte integers. Read more: Calvin Hsia's WebLog
0 comments:
Post a Comment