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.
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#
add a comment |
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.
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#
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
add a comment |
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.
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#
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.
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#
c#
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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