How to stop IPEndPoint.Serialize() from Garbage Collecting











up vote
0
down vote

favorite












I am having a small issue please click image to enlarge I am now positive that the IPEndPoint.Serialize call is coming from the Socket.SendTo method. I have commented that code out of my class. Now I am stuck wondering how do you avoid that from being garbage collected?? The profiler is from Unity3d but the code should not reflect the engine as I can run the API in Visual Studio.



Performance Analyzer



    public static void BroadCast(Packet packet, Protocol protocol, EndPoint exemptConnection = null)
{
lock (Rnet)
{
lock (Ipo.PeerData)
{
if (Ipo.PeerData.Count == 0)
{
Recycle(packet);
return;
}

if (packet.Flag == PacketFlags.None)
packet.Flag = PacketFlags.Dat;

foreach (var i in Ipo.PeerData)
{
if (i.Equals(exemptConnection)) continue;

if (protocol == Protocol.Sequenced)
{
packet.RemoteEp = i.Key;
SetId(packet);
packet.Proto = protocol;
packet.WriteHeader();
}
//The issue is in the Socket.SendTo Method I commented it out to make sure
_socket.RnetSocket.SendTo(packet.Data.Array,packet.Data.Count, System.Net.Sockets.SocketFlags.None, i.Key);
}

if (packet.Proto != Protocol.ReliableOrdered)
Recycle(packet);
}
}
}


This class is always recycled back into a Queue after usage so GC will not collect on it. (I have confirmed the issue is not in this class)



public class Packet
{
public Protocol Proto = Protocol.Sequenced;
public PacketFlags Flag = PacketFlags.None;
public Fragment Fragmented = Fragment.None;
public EndPoint RemoteEp = new IPEndPoint(IPAddress.Any, 0);
private ushort _id;
public ushort Lead;
public int MssPackets;
}









share|improve this question




















  • 1




    I'm not sure i understand your problem. Serialize makes a string. A string should be GC'd when it's no longer being referenced.
    – Brian White
    Nov 9 at 6:36






  • 1




    I guess you could use some static string and lock around it as well to manage it's state, and always serialize into that string, and send that string in your send method. But is it really a problem? The string will be quickly GC'd, it won't survive multiple generations, so it should be fine
    – Brian White
    Nov 9 at 6:37










  • @BrianWhite, Thank you I looked at the source and its creating a new object referencesource.microsoft.com/#System/net/System/Net/… not sure why the socket would ask for an EndPoint only to converge it to a IPEndPoint.
    – Levon Ravel
    Nov 9 at 8:15















up vote
0
down vote

favorite












I am having a small issue please click image to enlarge I am now positive that the IPEndPoint.Serialize call is coming from the Socket.SendTo method. I have commented that code out of my class. Now I am stuck wondering how do you avoid that from being garbage collected?? The profiler is from Unity3d but the code should not reflect the engine as I can run the API in Visual Studio.



Performance Analyzer



    public static void BroadCast(Packet packet, Protocol protocol, EndPoint exemptConnection = null)
{
lock (Rnet)
{
lock (Ipo.PeerData)
{
if (Ipo.PeerData.Count == 0)
{
Recycle(packet);
return;
}

if (packet.Flag == PacketFlags.None)
packet.Flag = PacketFlags.Dat;

foreach (var i in Ipo.PeerData)
{
if (i.Equals(exemptConnection)) continue;

if (protocol == Protocol.Sequenced)
{
packet.RemoteEp = i.Key;
SetId(packet);
packet.Proto = protocol;
packet.WriteHeader();
}
//The issue is in the Socket.SendTo Method I commented it out to make sure
_socket.RnetSocket.SendTo(packet.Data.Array,packet.Data.Count, System.Net.Sockets.SocketFlags.None, i.Key);
}

if (packet.Proto != Protocol.ReliableOrdered)
Recycle(packet);
}
}
}


This class is always recycled back into a Queue after usage so GC will not collect on it. (I have confirmed the issue is not in this class)



public class Packet
{
public Protocol Proto = Protocol.Sequenced;
public PacketFlags Flag = PacketFlags.None;
public Fragment Fragmented = Fragment.None;
public EndPoint RemoteEp = new IPEndPoint(IPAddress.Any, 0);
private ushort _id;
public ushort Lead;
public int MssPackets;
}









share|improve this question




















  • 1




    I'm not sure i understand your problem. Serialize makes a string. A string should be GC'd when it's no longer being referenced.
    – Brian White
    Nov 9 at 6:36






  • 1




    I guess you could use some static string and lock around it as well to manage it's state, and always serialize into that string, and send that string in your send method. But is it really a problem? The string will be quickly GC'd, it won't survive multiple generations, so it should be fine
    – Brian White
    Nov 9 at 6:37










  • @BrianWhite, Thank you I looked at the source and its creating a new object referencesource.microsoft.com/#System/net/System/Net/… not sure why the socket would ask for an EndPoint only to converge it to a IPEndPoint.
    – Levon Ravel
    Nov 9 at 8:15













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am having a small issue please click image to enlarge I am now positive that the IPEndPoint.Serialize call is coming from the Socket.SendTo method. I have commented that code out of my class. Now I am stuck wondering how do you avoid that from being garbage collected?? The profiler is from Unity3d but the code should not reflect the engine as I can run the API in Visual Studio.



Performance Analyzer



    public static void BroadCast(Packet packet, Protocol protocol, EndPoint exemptConnection = null)
{
lock (Rnet)
{
lock (Ipo.PeerData)
{
if (Ipo.PeerData.Count == 0)
{
Recycle(packet);
return;
}

if (packet.Flag == PacketFlags.None)
packet.Flag = PacketFlags.Dat;

foreach (var i in Ipo.PeerData)
{
if (i.Equals(exemptConnection)) continue;

if (protocol == Protocol.Sequenced)
{
packet.RemoteEp = i.Key;
SetId(packet);
packet.Proto = protocol;
packet.WriteHeader();
}
//The issue is in the Socket.SendTo Method I commented it out to make sure
_socket.RnetSocket.SendTo(packet.Data.Array,packet.Data.Count, System.Net.Sockets.SocketFlags.None, i.Key);
}

if (packet.Proto != Protocol.ReliableOrdered)
Recycle(packet);
}
}
}


This class is always recycled back into a Queue after usage so GC will not collect on it. (I have confirmed the issue is not in this class)



public class Packet
{
public Protocol Proto = Protocol.Sequenced;
public PacketFlags Flag = PacketFlags.None;
public Fragment Fragmented = Fragment.None;
public EndPoint RemoteEp = new IPEndPoint(IPAddress.Any, 0);
private ushort _id;
public ushort Lead;
public int MssPackets;
}









share|improve this question















I am having a small issue please click image to enlarge I am now positive that the IPEndPoint.Serialize call is coming from the Socket.SendTo method. I have commented that code out of my class. Now I am stuck wondering how do you avoid that from being garbage collected?? The profiler is from Unity3d but the code should not reflect the engine as I can run the API in Visual Studio.



Performance Analyzer



    public static void BroadCast(Packet packet, Protocol protocol, EndPoint exemptConnection = null)
{
lock (Rnet)
{
lock (Ipo.PeerData)
{
if (Ipo.PeerData.Count == 0)
{
Recycle(packet);
return;
}

if (packet.Flag == PacketFlags.None)
packet.Flag = PacketFlags.Dat;

foreach (var i in Ipo.PeerData)
{
if (i.Equals(exemptConnection)) continue;

if (protocol == Protocol.Sequenced)
{
packet.RemoteEp = i.Key;
SetId(packet);
packet.Proto = protocol;
packet.WriteHeader();
}
//The issue is in the Socket.SendTo Method I commented it out to make sure
_socket.RnetSocket.SendTo(packet.Data.Array,packet.Data.Count, System.Net.Sockets.SocketFlags.None, i.Key);
}

if (packet.Proto != Protocol.ReliableOrdered)
Recycle(packet);
}
}
}


This class is always recycled back into a Queue after usage so GC will not collect on it. (I have confirmed the issue is not in this class)



public class Packet
{
public Protocol Proto = Protocol.Sequenced;
public PacketFlags Flag = PacketFlags.None;
public Fragment Fragmented = Fragment.None;
public EndPoint RemoteEp = new IPEndPoint(IPAddress.Any, 0);
private ushort _id;
public ushort Lead;
public int MssPackets;
}






c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 6:21

























asked Nov 9 at 4:14









Levon Ravel

1805




1805








  • 1




    I'm not sure i understand your problem. Serialize makes a string. A string should be GC'd when it's no longer being referenced.
    – Brian White
    Nov 9 at 6:36






  • 1




    I guess you could use some static string and lock around it as well to manage it's state, and always serialize into that string, and send that string in your send method. But is it really a problem? The string will be quickly GC'd, it won't survive multiple generations, so it should be fine
    – Brian White
    Nov 9 at 6:37










  • @BrianWhite, Thank you I looked at the source and its creating a new object referencesource.microsoft.com/#System/net/System/Net/… not sure why the socket would ask for an EndPoint only to converge it to a IPEndPoint.
    – Levon Ravel
    Nov 9 at 8:15














  • 1




    I'm not sure i understand your problem. Serialize makes a string. A string should be GC'd when it's no longer being referenced.
    – Brian White
    Nov 9 at 6:36






  • 1




    I guess you could use some static string and lock around it as well to manage it's state, and always serialize into that string, and send that string in your send method. But is it really a problem? The string will be quickly GC'd, it won't survive multiple generations, so it should be fine
    – Brian White
    Nov 9 at 6:37










  • @BrianWhite, Thank you I looked at the source and its creating a new object referencesource.microsoft.com/#System/net/System/Net/… not sure why the socket would ask for an EndPoint only to converge it to a IPEndPoint.
    – Levon Ravel
    Nov 9 at 8:15








1




1




I'm not sure i understand your problem. Serialize makes a string. A string should be GC'd when it's no longer being referenced.
– Brian White
Nov 9 at 6:36




I'm not sure i understand your problem. Serialize makes a string. A string should be GC'd when it's no longer being referenced.
– Brian White
Nov 9 at 6:36




1




1




I guess you could use some static string and lock around it as well to manage it's state, and always serialize into that string, and send that string in your send method. But is it really a problem? The string will be quickly GC'd, it won't survive multiple generations, so it should be fine
– Brian White
Nov 9 at 6:37




I guess you could use some static string and lock around it as well to manage it's state, and always serialize into that string, and send that string in your send method. But is it really a problem? The string will be quickly GC'd, it won't survive multiple generations, so it should be fine
– Brian White
Nov 9 at 6:37












@BrianWhite, Thank you I looked at the source and its creating a new object referencesource.microsoft.com/#System/net/System/Net/… not sure why the socket would ask for an EndPoint only to converge it to a IPEndPoint.
– Levon Ravel
Nov 9 at 8:15




@BrianWhite, Thank you I looked at the source and its creating a new object referencesource.microsoft.com/#System/net/System/Net/… not sure why the socket would ask for an EndPoint only to converge it to a IPEndPoint.
– Levon Ravel
Nov 9 at 8:15

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219784%2fhow-to-stop-ipendpoint-serialize-from-garbage-collecting%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219784%2fhow-to-stop-ipendpoint-serialize-from-garbage-collecting%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()