在这里需要一些帮助! (VB.NET) [英] Need some help here !!! (VB.NET)

查看:90
本文介绍了在这里需要一些帮助! (VB.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我在v.net中尝试了以下代码

hey guys,
i tried the following code in v.net

 Imports System.Runtime.InteropServices
Module afa
    <DllImport("kernel32.dll")> Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)

    End Sub
End Module
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim test As String = "bguksjgfbusglhnhbusiytghbusghnsubhkg6styghdbsgjsyghuis"
        Dim gch As GCHandle = GCHandle.Alloc(test, GCHandleType.Pinned)
        Dim test2 As String = test


        ZeroMemory(gch.AddrOfPinnedObject, test.Length * 2)
        gch.Free()

        My.Computer.FileSystem.WriteAllText("C:\dos\test1.txt", test, False)

        My.Computer.FileSystem.WriteAllText("C:\test2.txt", test2, False)



    End Sub
End Class


并且我注意到在删除内存中的"test"内容后,test2的内容也被删除了! 你能告诉我为什么会这样吗?
哦...我还可以对ZeroMemory对变量的作用提供一些帮助,为什么我不能对其他对象(如
)使用Zeromemory Stringbuilders以及我如何释放内存"!
当我说空闲内存"时,我的意思是:


and I noticed that after deleting the contents of "test" off the memory the contents of test2 were deleted to !!
can you tell me why this happens ?
ohh... I''d also could use some help about what ZeroMemory does to the variables, Why cant I use zeromemory to other objects like
Stringbuilders and how cann i "free memory " !
when i say "free memory" i mean this :

Imports System.Runtime.InteropServices
Module afa
    <DllImport("kernel32.dll")> Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
    End Sub
End Module
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim stra As New System.Text.StringBuilder
        Do
            Try
                stra.Append("FAFA")
            Catch ex As Exception
                Exit Do
            End Try
        Loop
        stra = New System.Text.StringBuilder
    End Sub
End Class


如果您注意到taskmanager,即使我重新初始化stringbuilder并删除其所有内容后,程序使用的内存仍处于高级状态(大约210.000 KB)! 为什么呢????
希望大家能帮忙!
这是我见过的最好的编程论坛之一,所以我有希望!
提前致谢 !!! :)


if you notice the taskmanager, the memory used by the program is still at high-level (about of 210.000 kbytes) even after i re-initialize the stringbuilder and delete all of its contents !
why is that ????
I hope you guys can help !
it''s one of the bests programing forums I''ve ever seen so I have hopes !!!
thanks in advance !!! :)

推荐答案

添加到CG的响应中.


这是因为字符串实习.两个字符串都将指代相同的内部字符串,因此通过直接内存访问更改一个字符串将同时更改两个字符串.在这种情况下,找出问题很容易,因为两个字符串句柄都在同一代码块中,但是CLR在应用程序域之间执行字符串实习.这意味着更改实习字符串可能会导致应用程序完全断开连接的部分中极难调试的错误.因此,请尝试避免这样做,除非您真的知道自己在做什么.
Adding to CG''s response.


It''s because of string interning. Both strings will be referring to the same interned string, so changing one via direct memory access will change both. In this case, figuring out the problem was easy, because both string handles were in the same block of code, but the CLR performs string interning across application domains. This means changing an interned string can result in extremely hard-to-debug errors in totally disconnected parts of your application. So try and avoid doing it, unless you really know what you are doing.


我将来会建议使用有意义的标头.在此处发布的任何人都可以有效地使用请帮助".

如果可以的话,您永远不要费心固定或控制内存. .Net为您完成所有这些工作.因此,您的两个字符串被清除了,因为它们是对一个对象的引用,您可能应该阅读在线文章或书籍,其中介绍了.NET内存管理和垃圾回收.
I would suggest in future using a meaningful header. Anyone who posts here could validly use ''need some help please''.

You should never bother with pinning or controlling memory if you can help it. .Net does all of that for you. So, your two strings are cleared because they are references to the one object, and you should probably read an online article or book that explains .NET memory management and garbage collection.


这篇关于在这里需要一些帮助! (VB.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆