21 ms·
Memory usage of a toy C# server and client with 500K concurrent connections on
- kerng 8y agoI always liked C#. It's probably the nicest language to work with. Worked with golang recently a bit and its like going back to the 90s or 80s. It's just a little late with all the open sourcing, I just hope besides enterprise devs, folks would see the value also - most revs I've spoken to that dont like it havent actually even touched C#.
- highace 8y agoI see the same - lots of devs with opinions of M$ and "isn't ASP.NET that thing with forms and callbacks". Too bad for them!
- jchw 8y agoVery nice. I had looked into using tasks for TCP servers written in dotnet core C# recently. I find it mildly annoying that some of the async socket IO in C# is not easily awaitable, but after working around that I found it fairly easy. All in all, I see a good future for dotnet core. I can recommend the Jetbrains Rider IDE to go with it.
- taspeotis 8y ago> some of the async socket IO in C# is not easily awaitable Do any examples come to mind? I thought Microsoft did a pretty thorough job of adding async support to nearly all of the BCL in .NET 4.5. Not saying there aren't gaps, I'm just curious. Sockets' non-blocking model is an excellent candidate for async/await-ifying.
- jchw 8y agoThe methods on Socket don't have async methods. As others have pointed out, you can use NetworkStream. I have missed the memo on that :)
- int_19h 8y agoThere were plenty of gaps really. Async'ification is still ongoing, but I don't think we'll ever see it in classic .NET - Core is where it's at these days.
- benaadams 8y agoI think the pattern is to give the socket to a `NetworkStream` constructor, then it has all the regular async methods on it.
- oaiey 8y agoSearch for system.io.pipeline. big stuff is coming there.
- me551ah 8y agoC# is a pretty underrated language. I've worked with JS, Java, C++, Perl and I've always preferred to use C# for personal projects. async/await - C# is the ONLY language which gets this right. Javascript has it now but you need to be use libraries which support it too, which are hard to find. Kotlin has coroutines but Java base library doesn't have support for coroutines so you can't use it all the way through and create a chain of all async functions. Once you use async/await it's hard to go back, since the code is as easy to write as synchronous code. Then you have proper support for generics(unlike Java which has type erasure), LINQ queries and an amazing IDE which is visual studio.
- mr_toad 8y agoI’ve always liked C#. But official support outside Windows is only very recent. And .NET is still very much Microsoft’s baby. Unlike an independent language like C or JS or Rust, MS could pull the rug out at any time. Yes, Mono. But Mono is an unofficial port of a moving target. They have no control over the direction of .NET.
- benaadams 8y ago.NET Foundation's baby now https://dotnetfoundation.org/ https://dotnetfoundation.org/ With open membership https://dotnetfoundation.org/blog/2018/12/04/announcing-net-foundation-open-membership https://dotnetfoundation.org/blog/2018/12/04/announcing-net-... and election of its board https://dotnetfoundation.org/blog/2019/01/23/why-you-should-join-part-3-become-a-board-member https://dotnetfoundation.org/blog/2019/01/23/why-you-should-...
- mr_toad 8y agoThe board hasn’t even been elected yet! Maybe MS will in future cede all control over the language. Maybe in future 99% of code contributions won’t come from Microsoft employees. But even then it would take years for the language to become more widely adopted outside Windows.
- mandeepj 8y ago
- cryptozeus 8y agoWe just upgraded from .net 4.6 to .net core , without touching any logic change we are getting 30% performance upgrade. .net core is game changer within .net community.
- CuriousSkeptic 8y agoI think you could expect similar gains going to .net 4.7.2 though most optimizations done for core has been implemented and enabled for .net to.
- mavelikara 8y agoWhat do you attribute the gains to?
- hugh4life 8y agoA few days ago I was checking the former computer language shootout for another language and saw that C# is beating Java on all the benchmarks. This surprised me because I always had remembered Java being slightly ahead. https://benchmarksgame-team.pages.debian.net/benchmarksgame/faster/csharp.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... Can someone more knowledgeable give me some perspective on how representative these benchmarks are? At this point would it be wrong to say that in general the C# and .net environment is more performant than Java? If so, will there be any changes to Java to bridge any gap?
- nicoburns 8y agoI've seen multiple reports of large performance improvements (like ~30%) when moving from the old .Net to .Net Core (the newer, open source version). I imagine this would be more than enough of a difference to overtake Java. C# has a number of features (like `Span<T>` and value types) which Java doesn't currently have, which enable programs to run more efficiently, which might explain the difference. IIRC there are plans to add value types to Java, but they're not ready yet.
- codexon 8y agoI think the .net implementation benchmarked before was using mono, that's why Java was ahead before.
- rlp 8y agoWell, I would say that it highly depends on your workload. .NET's biggest performance win is lower-level memory management, like structs and Spans. If you can use those to avoid allocations and improve memory locality in your critical path, you'll end up beating the JVM. For allocation-heavy workloads that can't use .NET's lower-level memory tools, I think the JVM is still a bit ahead with its better GC and profiling HotSpot JIT compiler.
- nqzero 8y agothat benchmark is terrible (see the github for details). for k-nucleotides, the test that showed the biggest delta (c# vs java), i was able to improve the java perf by 35%. but the test harness is so poorly done that i didn't bother running it against the other impls. the reality is that good java developers just don't care about this sort of thing (which may say something unfortunate about me) and the benchmark is bad https://github.com/nqzero/k-nucleotide https://github.com/nqzero/k-nucleotide
- jarym 8y agoI'm familiar with both Java and .NET worlds and for a recent product had to pick one. I ended up picking Kotlin (JVM) only because the team I'm working with are more proficient in Java but I must admit, C# looked incredibly tempting technologically. Java won't go away any time soon but C#/.NET seems to be under better and more determined leadership.
- maxpert 8y agoHave used C# before for almost 5 years. Develeoped for backend and frontend both! Love the language, love TPL. Kotlin in my opinion brings a better coroutine implementation. I love they way I can intercept contexts and arrange them, letting me have go-ish selects, and the structured concurrency. I think Kotlin might take it a level higer.
- userbinator 8y agoObviously C/C++/Rust with handwritten state machines could make this way smaller, e.g. under 100 bytes. But at some point there are diminishing returns, and also developer productivity, bugs, and security are big tradeoffs. Looking at this another way: you need >20x more memory to achieve the same level of performance as native code in C#. Suddenly the benchmark doesn't look that great anymore.
- karl_p 8y agoIt still will be amortized by your application's data. For example, reading and buffering actual messages. The benchmark literally just has a one byte "buffer" per connection. I threw this benchmark together in a couple of hours pretty easily. The most annoying part was just measuring the RAM :-) But yes, everything is a tradeoff. If you want safety, you pay more - either in RAM, or in cognitive load.
- cwyers 8y ago20x on a toy problem isn't bad, if some portion of the increased memory usage is fixed (in other words, doesn't scale up with the complexity of the app).
- nicoburns 8y agoTrue, on the other hand if C#'s memory usage is reasonable enough that your program isn't memory bound, then it might start looking better again.
- int_19h 8y ago> Looking at this another way: you need >20x more memory to achieve the same level of performance as native code in C#. It's more like, you need 10x more memory with 100x more productivity than C/C++. If you don't care about productivity, however, you can actually write C# as you'd write C, and with a very similar perf profile. You can put everything into structs to avoid heap allocations, for example. You could use stackalloc, which basically gives you fixed-size non-bounds-checked stack-allocated arrays, exactly as in C. You can have pointers that GC knows nothing about, and you can even do arithmetic on them like in C. You can even have type punning unions. And then you can put an API on top of all that which makes it usable with async/await - and write the rest of your code in high-level C#.
- Thaxll 8y agoThat thing is not using TCP...
- karl_p 8y agoCorrect, hard to use TCP for 500K connections for a quick test when there are only 64K source ports which suffer from TIME_WAIT issues as bonus. It uses Unix Stream sockets.
- altmind 8y ago64k ports is not a limitation for number of simultaneous tcp connections, in linux kernel ports are identified by (srcip,srcport,dstip,dstport)
- abbeyj 8y agoCan you distribute your connections across 127.0.0.1, 127.0.0.2, 127.0.0.3, etc.? You've got the whole class A reserved as the loopback which should give you plenty of ports to work with.
- dmarlow 8y agoIt says that in the doc.
- faitswulff 8y agoI know nothing of C#, but there are a lot of comparisons to Java in this thread. Does C# have anything comparable to the JVM? That is, does anything compile to C# bytecode, if there is such a thing?
- josephcooney 8y agoYes, since the get-go .NET has targeted a VM called the CLR (the common language runtime). The biggest difference between the JVM and the CLR (initially) was that Microsoft designed the CLR to be mutli-language, whereas the JVM was written "just for java".
- mr_custard 8y agoYes. The CLR (Common Language Runtime) is like the JVM and support multiple languages, hence the name. C# compiles to Common Intermediate Language (CIL) which is then assembled into a form of bytecode.
- twodave 8y agoTIL MSIL is now called CIL. What the hell!
- zokier 8y agoIt has been called CIL close to a decade now..
- pauldino 8y agoThe first edition of ECMA-335 was published December 2001, so now almost two!
- twodave 8y agoYep, that was the most surprising thing to me. In my circles people have just continued calling it MSIL all this time and I’ve never questioned it or needed to research anything MSIL related so I guess it slipped past me all this time. Hence the “what the hell!”
- aliswe 8y agoI love C# and .NET so much. But I dislike some of the stuff being done in .NET Core, for example the compatibility flags instead of using semver. And integratig GDPR? IN CORE? VS just feels like a big fat 32-bit legacy application with no support for long filenames. Its very hard to do improvements for them, that's for sure. Just look at NuGets package manager UI. Surely there are so many improvements one could make. (Compare that with the Kaizen feeling you get with each VS Code update) I feel MS marketing has too much to say and pushes unnecessary stuff into the UI. Bring in clippy already! The tooling for Razor is so extremely buggy, automatic indentation is broken, try copying pasting imports, changing model type requires reopening of window, dependent on folder-local web.config to give correct analysis (but still may compile at runtime) Also, Im afraid of the frequent updates should something break. (Best to wait them out a few weeks.) Then you have the seemingly impossible task of uninstalling it. Better just reinstall windows itself. That being said, I guess I feel like they're doing their best on many points. The code base must really be huge and they are no doubt pouring in lots of resources improving vs. I was even called once from the US by a womam asking me for ideas on how they could improve the break-on-exceptions experience while debugging! I mean who would do that. And some areas are totally wonderful and unheard of in other platforms, for example sourceview with full integration with github. I dont think many know how far ahead .net is on so many levels!
- rhplus 8y agoAnd integratig GDPR? IN CORE? I was puzzled by this comment. From what I can tell, the GDPR stuff is an optional templating feature in the ASP.NET Core, not a feature of .NET Core. https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.2 https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?v...
- james_s_tayler 8y agoYeah that's how I understand it too. I thought it was a nice addition and forward thinking to include it.
- kuminoter 8y agoDoes that mean there is a spyware included in the .NET Core?
- rb808 8y agoI moved from C# to Java world - similar but very different. Java has way more libraries and frameworks which is great, but it almost makes the complexity worse as there are n different Javas. The biggest thing is memory management. I never had trouble with GC in dotnet, Java locks up with full GCs WTF.
- altmind 8y agoCan somebody enlighten me if official microsoft csc is available for linux? Last time i checked there was mono with some old C# standard and there was .net core suite... for freebsd only(due to licensing?)
- paavohtl 8y agoThis must have been some time ago. The .NET runtime / C# compiler you're thinking of is Rotor, a version of the runtime for educational use released back in 2006. Mono has followed the official implementation quite closely for at least the past decade. The new official C# compiler - Roslyn - was released in 2014. It's open source and cross-platform. It was followed up same year by .NET Core, which is the new primary distribution of .NET with support for Windows, Linux, macOS and a handful of [other] BSD derivatives. Sources: https://github.com/dotnet/roslyn https://github.com/dotnet/roslyn https://github.com/dotnet/coreclr https://github.com/dotnet/coreclr https://github.com/dotnet/corefx https://github.com/dotnet/corefx Binaries: https://dotnet.microsoft.com/download https://dotnet.microsoft.com/download
- freecodyx 8y agoC# is such an expressive language.
- kuminoter 8y agoWhich makes it tempting to write a code nobody understands but the author.
- kuminoter 8y agoAnything coming from M$ likely has telemetry (read spyware) sneaked in, so by the time their packages are audited you would have delivered your software with different technology. I wouldn't work for companies using unethical tactics anyway.