C++ process is leaking memory slowly

Question: One of our process written in C++ is leaking the memory very slowly. Valgrind is showing 0 bytes in all 3 sections (definitely lost, indirectly lost, & possibly lost). This issue is taking nearly about 6-7 months to consume 70% of systems memory with 8GB of RAM. I tried to reproduce the issue on test machine and was able to see memory consumption slowly after many requests.
Below is the valgrind results that I got:
==20868==
--20868-- REDIR: 0x4019f70 (ld-linux-x86-64.so.2:strlen) redirected to 0x58059dd1 (???)
--20868-- REDIR: 0x4019d40 (ld-linux-x86-64.so.2:index) redirected to 0x58059deb (???)
--20868-- Reading syms from /usr/lib64/valgrind/vgpreload_core-amd64-linux.so
--20868-- Reading syms from /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
==20868== WARNING: new redirection conflicts with existing -- ignoring it
--20868--     old: 0x04019f70 (strlen              ) R-> (0000.0) 0x58059dd1 ???
--20868--     new: 0x04019f70 (strlen              ) R-> (2007.0) 0x04c2ba90 strlen
--20868-- REDIR: 0x4019ef0 (ld-linux-x86-64.so.2:strcmp) redirected to 0x4c2cbe0 (strcmp)
--20868-- REDIR: 0x401aae0 (ld-linux-x86-64.so.2:mempcpy) redirected to 0x4c2fc60 (mempcpy)
--20868-- Reading syms from /lib64/libpthread-2.17.so
--20868--   Considering /usr/lib/debug/.build-id/d9/73c39d1900dc61d8519c653c3bc405692de563.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /usr/lib64/libcurl.so.4.4.0
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libcassandra.so.2.9.0
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libstdc++.so.6.0.24
--20868--   Considering /usr/lib/debug/.build-id/87/91ddd49348603cd50b74652c5b25354d8fd06e.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /lib64/libm-2.17.so
--20868--   Considering /usr/lib/debug/.build-id/07/fb92afef1756f093371ce60c3ae85dd3a06325.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /lib64/libgcc_s-7-20170915.so.1
--20868--   Considering /usr/lib/debug/.build-id/a0/3c9a80e995ed5f43077ab754a258fa0e34c3cd.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /lib64/libc-2.17.so
--20868--   Considering /usr/lib/debug/.build-id/af/310f56618fc1ef9158973484f60942f11cc0fb.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /usr/lib64/libnghttp2.so.14.13.1
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libidn2.so.0.1.4
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libssh2.so.1.0.1
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libpsl.so.0.2.3
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libssl3.so
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libsmime3.so
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libnss3.so
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libnssutil3.so
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /lib64/libplds4.so
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /lib64/libplc4.so
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /lib64/libnspr4.so
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /lib64/libdl-2.17.so
--20868--   Considering /usr/lib/debug/.build-id/0e/5cd5baa5ee8bf3648a5031b088f9a78c89364f.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /usr/lib64/libgssapi_krb5.so.2.2
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libkrb5.so.3.3
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libk5crypto.so.3.1
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libcom_err.so.2.1
--20868--   Considering /usr/lib/debug/.build-id/ab/007f5df96c66e515542598f5be1429ed63d86f.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /lib64/liblber-2.4.so.2.10.3
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /lib64/libldap-2.4.so.2.10.3
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /lib64/libz.so.1.2.8
--20868--   Considering /usr/lib/debug/.build-id/89/c6af118b6b4fb6a73ae1813e2c8bdd722956d1.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /usr/lib64/libuv.so.1.0.0
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libssl.so.1.0.2k
--20868--   Considering /usr/lib/debug/.build-id/9c/4eb34a346260f2a77746f4e5ed837619137db7.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /usr/lib64/libcrypto.so.1.0.2k
--20868--   Considering /usr/lib/debug/.build-id/0b/7f7487280fe68af9302a282fae37776a99bc80.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /usr/lib64/libunistring.so.0.1.2
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libicuuc.so.50.1.2
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /lib64/librt-2.17.so
--20868--   Considering /usr/lib/debug/.build-id/f2/701e2a24459d5b55df5549d585f091e7bcf07a.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /usr/lib64/libkrb5support.so.0.1
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /lib64/libkeyutils.so.1.5
--20868--   Considering /usr/lib/debug/.build-id/37/a58210fa50c91e09387765408a92909468d25b.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /lib64/libresolv-2.17.so
--20868--   Considering /usr/lib/debug/.build-id/9e/5e0bf5f22de7555bc4b9853240817147489258.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /usr/lib64/libsasl2.so.2.0.23
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /lib64/libnsl-2.17.so
--20868--   Considering /usr/lib/debug/.build-id/af/2bb3cd9fcda0b820c88aab5d80639ca5287921.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /usr/lib64/libicudata.so.50.1.2
--20868--    object doesn't have a symbol table
--20868-- Reading syms from /usr/lib64/libselinux.so.1
--20868--   Considering /usr/lib/debug/.build-id/f5/054dc94443326819fbf3065cfdf5e4726f57ee.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /lib64/libcrypt-2.17.so
--20868--   Considering /usr/lib/debug/.build-id/9a/9be1d2234d3641b9cfe17bb7a40b82973a2802.debug ..
--20868--   .. build-id is valid
--20868-- Reading syms from /lib64/libfreebl3.so
--20868--    object doesn't have a symbol table
--20868-- REDIR: 0x5f79e40 (libc.so.6:strcasecmp) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x5f76bc0 (libc.so.6:strnlen) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x5f7c110 (libc.so.6:strncasecmp) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x5f79620 (libc.so.6:memset) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x5f795d0 (libc.so.6:memcpy@GLIBC_2.2.5) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x5f75040 (libc.so.6:strcmp) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x5f76ae0 (libc.so.6:__GI_strlen) redirected to 0x4c2b9f0 (__GI_strlen)
--20868-- REDIR: 0x5f785b0 (libc.so.6:__GI_strrchr) redirected to 0x4c2b450 (__GI_strrchr)
--20868-- REDIR: 0x5f6f800 (libc.so.6:malloc) redirected to 0x4c28b5c (malloc)
--20868-- REDIR: 0x5f8f630 (libc.so.6:__GI_strstr) redirected to 0x4c2fef0 (__strstr_sse2)
--20868-- REDIR: 0x5f78cb0 (libc.so.6:memchr) redirected to 0x4c2cc80 (memchr)
--20868-- REDIR: 0x5f7e850 (libc.so.6:__GI_memcpy) redirected to 0x4c2d580 (__GI_memcpy)
--20868-- REDIR: 0x5f8fbf0 (libc.so.6:strstr) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x602a620 (libc.so.6:__strstr_sse42) redirected to 0x4c2ff80 (__strstr_sse42)
--20868-- REDIR: 0x5f6fc20 (libc.so.6:free) redirected to 0x4c29c56 (free)
--20868-- REDIR: 0x5f76a90 (libc.so.6:strlen) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x6030670 (libc.so.6:__strlen_sse42) redirected to 0x4c2ba50 (__strlen_sse42)
--20868-- REDIR: 0x56dd640 (libstdc++.so.6:operator new(unsigned long)) redirected to 0x4c2917c (operator new(unsigned long))
--20868-- REDIR: 0x5f7e7e0 (libc.so.6:memcpy@@GLIBC_2.14) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x6036480 (libc.so.6:__memcpy_ssse3) redirected to 0x4c2cfa0 (memcpy@@GLIBC_2.14)
--20868-- REDIR: 0x5f74f80 (libc.so.6:index) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x6028640 (libc.so.6:__strchr_sse42) redirected to 0x4c2b640 (index)
--20868-- REDIR: 0x56db6f0 (libstdc++.so.6:operator delete(void*)) redirected to 0x4c2a106 (operator delete(void*))
--20868-- REDIR: 0x5f79040 (libc.so.6:__GI_memcmp) redirected to 0x4c2e4d0 (__GI_memcmp)
--20868-- REDIR: 0x60286f0 (libc.so.6:__strcmp_sse42) redirected to 0x4c2cb90 (__strcmp_sse42)
--20868-- REDIR: 0x5f70210 (libc.so.6:calloc) redirected to 0x4c2a8df (calloc)
--20868-- REDIR: 0x5f800d0 (libc.so.6:strchrnul) redirected to 0x4c2f780 (strchrnul)
--20868-- REDIR: 0x5f797f0 (libc.so.6:__GI_mempcpy) redirected to 0x4c2f990 (__GI_mempcpy)
--20868-- REDIR: 0x5f74d80 (libc.so.6:strcat) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x5f88a90 (libc.so.6:__strcat_ssse3) redirected to 0x4c2b680 (strcat)
--20868-- REDIR: 0xffffffffff600400 (???:???) redirected to 0x58059dbd (???)
--20868-- REDIR: 0x5f79680 (libc.so.6:__GI_memset) redirected to 0x4c2ee80 (memset)
--20868-- REDIR: 0xffffffffff600000 (???:???) redirected to 0x58059db3 (???)
--20868-- REDIR: 0x5f79000 (libc.so.6:bcmp) redirected to 0x4a24780 (_vgnU_ifunc_wrapper)
--20868-- REDIR: 0x6053910 (libc.so.6:__memcmp_sse4_1) redirected to 0x4c2e610 (__memcmp_sse4_1)
--20868-- REDIR: 0x5f79440 (libc.so.6:__GI_memmove) redirected to 0x4c2f1d0 (__GI_memmove)
--20868-- REDIR: 0x5f7fec0 (libc.so.6:__GI___rawmemchr) redirected to 0x4c2f7e0 (__GI___rawmemchr)
--20868-- REDIR: 0x56dd700 (libstdc++.so.6:operator new[](unsigned long)) redirected to 0x4c29821 (operator new[](unsigned long))
--20868-- REDIR: 0x603b820 (libc.so.6:__memmove_ssse3) redirected to 0x4c2cd40 (memcpy@GLIBC_2.2.5)
--20868-- REDIR: 0x5f76ce0 (libc.so.6:__GI_strncmp) redirected to 0x4c2c220 (__GI_strncmp)
--20868-- REDIR: 0x5f79ce0 (libc.so.6:__GI_stpcpy) redirected to 0x4c2e8d0 (__GI_stpcpy)
--20868-- REDIR: 0x56db720 (libstdc++.so.6:operator delete[](void*)) redirected to 0x4c2a5b6 (operator delete[](void*))
==20868== Warning: invalid file descriptor -1 in syscall close()
==20868==    at 0x4E4281D: ??? (syscall-template.S:81)
==20868==    by 0x49969C: RTSPServer::~RTSPServer() (RTSPServer.cpp:394)
==20868==    by 0x4A9DB3: RTSPServerWithREGISTERProxying::~RTSPServerWithREGISTERProxying() (RTSPServer.cpp:2816)
==20868==    by 0x45D2F5: iv::Server::~Server() (Server.cpp:292)
==20868==    by 0x45D329: iv::Server::~Server() (Server.cpp:311)
==20868==    by 0x4804D9: MediaLookupTable::remove(char const*) (Media.cpp:151)
==20868==    by 0x480100: Medium::close(UsageEnvironment&, char const*) (Media.cpp:53)
==20868==    by 0x48013F: Medium::close(Medium*) (Media.cpp:59)
==20868==    by 0x45CE03: iv::Server::releaseServer(iv::Server*&) (Server.cpp:269)
==20868==    by 0x45A5B8: main (Main.cpp:543)
==20868==
==20868== FILE DESCRIPTORS: 5 open at exit.
==20868== Open file descriptor 2: /var/log/ivrtspproxyserver/log/ivrtspproxyserver.log
==20868==    at 0x5FDA3A7: dup2 (syscall-template.S:81)
==20868==    by 0x476FBD: Logger::startNewSession(char) (Logger.cpp:712)
==20868==    by 0x4751CC: Logger::createLogFile(std::string, std::string, char) (Logger.cpp:441)
==20868==    by 0x4570FD: LogFile() (Main.cpp:84)
==20868==    by 0x458164: main (Main.cpp:283)
==20868==
==20868== Open file descriptor 1: /var/log/ivrtspproxyserver/log/ivrtspproxyserver.log
==20868==    at 0x5FDA3A7: dup2 (syscall-template.S:81)
==20868==    by 0x476FA7: Logger::startNewSession(char) (Logger.cpp:711)
==20868==    by 0x4751CC: Logger::createLogFile(std::string, std::string, char) (Logger.cpp:441)
==20868==    by 0x4570FD: LogFile() (Main.cpp:84)
==20868==    by 0x458164: main (Main.cpp:283)
==20868==
==20868== Open file descriptor 4: /var/log/ivrtspproxyserver/log/ivrtspproxyserver.log
==20868==    at 0x4E42F40: __open_nocancel (syscall-template.S:81)
==20868==    by 0x476E4B: Logger::startNewSession(char) (Logger.cpp:696)
==20868==    by 0x4751CC: Logger::createLogFile(std::string, std::string, char) (Logger.cpp:441)
==20868==    by 0x4570FD: LogFile() (Main.cpp:84)
==20868==    by 0x458164: main (Main.cpp:283)
==20868==
==20868== Open file descriptor 3: /home/ec2-user/akshay/mem_leak/IV-proxyserver/ProxyServer/leak_details.txt
==20868==    <inherited from parent>
==20868==
==20868== Open file descriptor 0: /dev/pts/2
==20868==    <inherited from parent>
==20868==
==20868==
==20868== HEAP SUMMARY:
==20868==     in use at exit: 261 bytes in 4 blocks
==20868==   total heap usage: 838 allocs, 834 frees, 289,041 bytes allocated
==20868==
==20868== Searching for pointers to 4 not-freed blocks
==20868== Checked 1,354,824 bytes
==20868==
==20868== 46 bytes in 1 blocks are still reachable in loss record 1 of 4
==20868==    at 0x4C29203: operator new(unsigned long) (vg_replace_malloc.c:334)
==20868==    by 0x571E858: allocate (new_allocator.h:111)
==20868==    by 0x571E858: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (basic_string.tcc:1057)
==20868==    by 0x57202E4: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (basic_string.tcc:578)
==20868==    by 0x572070B: _S_construct_aux<char const*> (basic_string.h:5031)
==20868==    by 0x572070B: _S_construct<char const*> (basic_string.h:5052)
==20868==    by 0x572070B: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (basic_string.tcc:665)
==20868==    by 0x4570BB: LogFile() (Main.cpp:84)
==20868==    by 0x458164: main (Main.cpp:283)
==20868==
==20868== 55 bytes in 1 blocks are still reachable in loss record 2 of 4
==20868==    at 0x4C29203: operator new(unsigned long) (vg_replace_malloc.c:334)
==20868==    by 0x571E858: allocate (new_allocator.h:111)
==20868==    by 0x571E858: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (basic_string.tcc:1057)
==20868==    by 0x57202E4: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (basic_string.tcc:578)
==20868==    by 0x572070B: _S_construct_aux<char const*> (basic_string.h:5031)
==20868==    by 0x572070B: _S_construct<char const*> (basic_string.h:5052)
==20868==    by 0x572070B: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (basic_string.tcc:665)
==20868==    by 0x4570E1: LogFile() (Main.cpp:84)
==20868==    by 0x458164: main (Main.cpp:283)
==20868==
==20868== 72 bytes in 1 blocks are still reachable in loss record 3 of 4
==20868==    at 0x4C29203: operator new(unsigned long) (vg_replace_malloc.c:334)
==20868==    by 0x4782AC: IVUtils::Object<IVUtils::Mutex>::CreateNew() (Object.h:21)
==20868==    by 0x4781DF: IVUtils::CreateMutex() (Mutex.cpp:73)
==20868==    by 0x474CCE: Logger::Logger() (Logger.cpp:404)
==20868==    by 0x456FEF: LogFile() (Main.cpp:74)
==20868==    by 0x458164: main (Main.cpp:283)
==20868==
==20868== 88 bytes in 1 blocks are still reachable in loss record 4 of 4
==20868==    at 0x4C29203: operator new(unsigned long) (vg_replace_malloc.c:334)
==20868==    by 0x456FE4: LogFile() (Main.cpp:74)
==20868==    by 0x458164: main (Main.cpp:283)
==20868==
==20868== LEAK SUMMARY:
==20868==    definitely lost: 0 bytes in 0 blocks
==20868==    indirectly lost: 0 bytes in 0 blocks
==20868==      possibly lost: 0 bytes in 0 blocks
==20868==    still reachable: 261 bytes in 4 blocks
==20868==                       of which reachable via heuristic:
==20868==                         stdstring          : 101 bytes in 2 blocks
==20868==         suppressed: 0 bytes in 0 blocks
==20868==
==20868== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==20868== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

What more can be the reason for such slow memory consumption?
Comment: According to your valgrind output, your program is *not* leaking any memory.  Therefore, if the program's RAM usage is growing over time, it must be that its data structures are getting larger.  Previously when I've seen this behavior, it turned out to be e.g. a `std::vector` that the code kept appending items to indefinitely, but never cleared or removed any items from.  (of course any variable-size data structure could have the same problem; I'm only using `std::vector` as a simple example)
Comment: Maybe it's not so much a memory leak as a data structure that grows gradually. Valgrind wouldn't necessarily show this as a leak.
Comment: So you think the problem is in `LogFile`? Would help to see that code.
Comment: would gdb help me to identify which data structure is growing gradually or which one is getting filled and is not cleared once use of that entry is done?
Comment: gdb is not as helpful as you'd like. You'd have to stop the program periodically and inspect all of the objects that COULD be growing and either justify or further inspect those that had grown since the last time you inspected. If you can accelerate the program to force a quicker consumption of resources this may be reasonable. If not you could find yourself running a debug instance for months before you spot and confirm the villain. Periodic logging of the size of the objects of interest may be more practical.
Comment: Hmm please remove the C++ tag, this has nothing to do with C++ !!!
Comment: @Yunfei Chen future readers may use the C++ tag to search for similar issues.
Comment: @PaulFloyd do you see any C++ code in there at all?? Cause I sure dont.....
Comment: The callstacks are clearly from C++.
Answer: Judging from your traces, you have 4 things still in use - logger, mutex for logger and two std::strings.
I suggest that you instrument your code with client requests (see here). That way you can see where memory is getting used incrementally. I particular, try using the VALGRIND_DO_ADDED_LEAK_CHECK macro.
On average, you are adding something like 300-350 bytes per second. Use your knowledge of the times for the logging events when running under Valgrind - you want it to run long enough to capture a few events at least.
Answer: One way to look at such bugs, where you believe that the likely cause is container growth, is to use https://github.com/vmware/chap
You don't need to instrument your process at all.  Just use gcore to gather a live core of your process.  Possibly do this more than once, say an hour apart.
For each core, open it in chap then try:
redirect on
summarize used

or
redirect on
summarize used /sortby bytes

Comparing the results will give you a sense of what sort of allocations are accumulating and you can look further at those.  Let's say, for example, that you discover that you are seeing an increase in allocations of type Foo.  You can then do
enumerate used Foo

to get a list of all the addresses of such allocations, then pick one such address and use something like the following repeatedly to figure out why they are held in memory:
describe incoming <address>

In that case the initial address you might use is one from the enumeration.

C++ process is leaking memory slowly


How to style html upload button and still have text on the side?

Question: i'm making a small photo upload website and I am trying to style the 'select file' button for the upload page. On the
<input type="file" name="uploadedfile">
 line I have tried adding a class to it and style it, but all that does it style the background, not the button itself. Does anyone know how to style the button, and keep the filename there? I want to style it kinda like the button and progressbar in this pic http://prntscr.com/3jcwmr
Comment: A lot of information can be found here: http://stackoverflow.com/questions/572768/styling-an-input-type-file-button
Comment: show us what you have tried so far .... only then it would be possible to help .... a fiddle would also help ...
Comment: You can't. You will have to style a different button and when that button is clicked, you fire the click event to the actually upload button
Comment: @ross thanks for that link, it's very useful
Answer: The workaround usually done for this is hide the uploadfile button. Create a customized good looking image or button and then write a javascript which onclick of this customized image or button actually clicks the hidden uploadfile button.
Comment: so if I do it this way, would I need to use `document.getElementById("files").click();` in the javascript to click the button when the other button is clicked? Also how would I get the filename to appear next to the custom button?


bijection $\{0, 1, \ldots, \binom{|m|}{n}-1\} \longleftrightarrow \binom{m}{n}$

Question: I am attempting to generate random numbers which correspond to states in a game. Say there are $|m|$ squares, and $n$ pieces which can occupy the squares. Then I can have a total of $\binom{|m|}{n}$ states, given the pieces are indistinct.
However, I would like to transform the randomly generated number $\left[0, \binom{|m|}{n}\right)$ into a specific state, and for this I need a bijection $\{0, 1, \ldots, \binom{|m|}{n}-1\}$ $\longleftrightarrow$ $\binom{m}{n}$.
An algorithm that I can think of (that may work) would in essence order the pieces, and place the $i^\text{th}$ piece on the $i^\text{th}$ square to start. Then iterate over the pieces, shifting the rightmost piece right if the square is not occupied. Whenever a piece $p_0$ to the left of any other given pieces $p_1, \ldots, p_k$ is shifted right, then shift $p_1, \ldots p_k$ left as far as possible such that $p_i$ is still on a lesser square than $p_j$.
I would appreciate guidance in any other directions or any simplifications etc!
Thanks!
Comment: I'm guessing you mean $m$ is the set of squares and $|m|$ is the number of squares. Can you confirm that? $\qquad$
Comment: You shouldn't keep alternating in and out of MathJax within an expression in mathematical notation. The whole of $\text{" } \{0, 1, \ldots, \binom{|m|}{n}-1\} \longleftrightarrow \binom{m}{n} \text{ ''}$ should be between just one pair of dollar signs. I edited accordingly. $\qquad$
Comment: But on the other hand, the first occurrence of $m$ in you posting seems to assume it's a number rather than a set.
Comment: @MichaelHardy Hi Michael, sorry for delayed response. Indeed your guess is correct. Thanks for latex advice. Ah yes, that's wrong. I guess I should have used $S$ and $|S|$, and then my opening statement could use $|S|$ as the number of pieces.
Answer: So from what I understand, you're trying to enumerate the set of size-$n$ subsets of some fixed set $\Omega$ of size $m$ so that you can readily compute the choice of size-$n$ subset solely from its index.
For simplicity, I will just assume $\Omega=\{0,\dots,m-1\}$ so that the states have a natural ordering to them.
With this, you can think of the set of size-$n$ subsets of $\Omega$ as length-$n$ strictly increasing sequences of numbers in $\Omega$, and you can order these lexicographically. This gives you one direction of the enumeration.
Now, we need only go backwards.
If we fix the first number of a length-$n$ increasing sequence to be $k$, then the remaining numbers have to fit in $\{k+1,\dots,m-1\}$---a set of size $m-k-1$, which means that there are ${m-k-1\choose n-1}$ length-$n$ increasing sequences which start with $k$ for any $0\leq k\leq m-1$.
This is the key for us to reverse-engineer the state from the index.
If we look at the subproblem of a length-$(n-1)$ increasing sequence of states in $\{k+1,\dots,m-1\}$, we can re-index everything by shifting down by $k+1$ and solving the recursive problem.
In summary, we can define a procedure $\def\getState{\operatorname{getState}}\getState(i,n,m,j)$ which prints the $i$th state in the lexicographical ordering of size-$n$ subsets of the set $\{j,\dots,j+m-1\}$ as follows:

if $n=0$, quit

the only size-$0$ subset of a set is empty

otherwise, let $k$ be maximal with $p := \sum_{j\leq k}{m-j-1\choose n-1}\leq i$ and print $(j+k)$

this finds the smallest element of the state

recursively call $\getState(i-p, n-1, m-k-1, j+k+1)$

the remaining problem is to find the $(i-p)$th state in the lexicographical ordering of the size-$(n-1)$ subsets of the set $\{j+k+1,\dots,j+m-1\}$

You can use memoisation to speed up the computation of the binomial coefficients (using Pascal's triangle).
With the above procedure, the answer to your original question will be $\getState(i,n,m,0)$.
Answer: Out of the $\binom mn$ possible $n$-element subsets of the $m$ squares, $\binom{m-1}{n-1}$ have a piece on the last square, and $\binom{m-1}{n}$ don't.
So you can do the following:

If there is no piece on the last square, use this algorithm recursively for the arrangement of $n$ pieces on the first $m-1$ squares. This gives you a number between $0$ and $\binom{m-1}{n}-1$.
If there is a piece on the last square, use this algorithm recursively for the arrangement of $n-1$ pieces on the first $m-1$ squares. This gives you a number between $0$ and $\binom{m-1}{n-1}-1$. Then, add $\binom{m-1}{n}$ to it, getting a number between $\binom{m-1}{n}$ and $\binom mn - 1$.

As a base case for this algorithm, whenever $m=n$ or $n=0$, there is only one possible arrangement, and you should give it the number $0$.

Actually, if we look carefully at the recursive algorithm above, we can "unroll" it. Note that we only have one place where we modify the number we get: when the $n^{\text{th}}$ piece is on the $m^{\text{th}}$ square, we add $\binom{m-1}{n}$ to the number. (We can simplify this rule by numbering the squares $0, 1, 2, \dots, m-1$, so that the $m-1$ in $\binom{m-1}{n}$ is the number of the last square.)
So if the squares are numbered $0, 1, 2, \dots, m-1$ and the pieces are located on squares $0 \le m_1 < m_2 < \dots < m_n \le m-1$, then the algorithm above will ultimately produce the value
$$
    \binom{m_1}{1} + \binom{m_2}{2} + \dots + \binom{m_n}{n}
$$
and we can just output that directly. (But without the recursive algorithm, it would be super unclear why that worked.)
Comment: that's genius :)


Two dimensional random variable with uniform marginal probability density functions

Question: I have access to some data for two variables - let's call them x and y.
In particular, I have the distribution of data separately per each variable, something that allows me to estimate the marginal distribution for each variable - let's call them $f_x(x)$ and $f_y(y)$.
From those histograms I see that both variables have a marginal distribution which is uniform: $x \sim f_x(x)=\frac{1}{x_b-x_a}$ and $y \sim f_y(y)=\frac{1}{y_b-y_a}$, where of course $$f_x(x)=\int_{y_a}^{y_b}dy f(x,y)$$ $$f_y(y)=\int_{x_a}^{x_b}dx f(x,y) $$
Can we conclude that the full pdf $f(x,y)$ is the product of the two marginals or at least a pdf exists such that its marginals distributions are uniform?
Many thanks in advance and kind regards.
Comment: Uniformity does not change anything to the missing information about the dependence between $X$ and $Y$. See https://stats.stackexchange.com/questions/64765/reconstructing-joint-distribution-from-marginals, https://stats.stackexchange.com/q/503988/7224, https://stats.stackexchange.com/q/324709/7224
Comment: See https://stats.stackexchange.com/questions/257779 for an interesting example. Other interesting examples are https://stats.stackexchange.com/questions/602028, and https://stats.stackexchange.com/questions/48086 (arbitrarily high dimensions!)
Answer: There is independence if and only if the product of the margins equals the joint density. You can find this shown in a classic book like Casella/Berger's Statistical Inference.
Thus, you are asking if the two distributions are guaranteed to be independent. The answer is that they are not guaranteed to be independent. They might be, but they might not be.
For some additional reading, you might be interested in the concept of a copula.
EDIT
It is fairly easy to construct a joint distribution with uniform margins that has considerable dependence, meaning that the joint distribution is not just the product of the margins. I will give a suggestive simulation below. Fleshing out the theoretical mathematics would mean getting into copulas.

library(ggplot2)
library(MASS)
set.seed(2023)

N <- 100

# Simulate some dependent data
# Will form an X shape
#
d0 <- MASS::mvrnorm(
    N,
    c(0, 0),
    matrix(
        c(
            1, 0.99,
            0.99, 1
        ), 2, 2
    )
)
d1 <- MASS::mvrnorm(
    N,
    c(0, 0),
    matrix(
        c(
            1, -0.99,
            -0.99, 1
        ), 2, 2
    )
)
d <- rbind(d0, d1)

#Transform the margins to be U(0, 1)
#
x <- ecdf(d[, 1])(d[, 1])
y <- ecdf(d[, 2])(d[, 2])

# Make data frames of the CDFs of the transformed x and y
#
df0 <- data.frame(
    Value = x,
    Quantile = ecdf(x)(x),
    Margin = "X"
)
df1 <- data.frame(
    Value = y,
    Quantile = ecdf(y)(y),
    Margin = "Y"
)
df_full <- rbind(df0, df1)

# Plot the margins to show they are uniform
#
ggplot(df_full, aes(x = Value, y = Quantile)) +
    geom_point() +
    geom_line() +
    geom_abline(slope = 1, intercept = 0) +
    facet_grid(~ Margin)

# Make a data frame for a scatter plot
#
df_scatter <- data.frame(
    X = x,
    Y = y
)

# Plot the joint distribution as a scatter plot
#
ggplot(df_scatter, aes(x = X, y = Y)) +
    geom_point()

The margins are $U(0, 1)$.

The joint distribution shows dependence.

Independence would look more like this.

You can create this by using sample(y) in the df_scatter, which breaks the dependence structure by scrambling the y values.
(I've actually used a concept from copulas to create this example, and readers will be able to pick up on it if they know copulas and the ecdf function, though the key point is that, regardless of how I created the joint distribution, the margins are uniform yet dependent.)
Comment: Thanks for the feedback. I am aware that the possibility to factorize the joint pdf is necessary and sufficient condition for the independence of the two variables.
I was wondering that since the two marginal distributions are uniform there is no way the joint is not just the product of the two.
And thanks, I will look at the concept of copula which I did not know.
Comment: Fantastic! Thank you so much!


Kali linux Source lists fails

Question: I am trying to update Kali Linux via the official source_lists while when i do sudo apt-get update it fails saying W: Failed to fetchThe source list is as follows :-
in /etc/apt/sources.list:
#regular repositories
deb http://http.kali.org/kali sana main non-free contrib
deb http://security.kali.org/kali-security sana/updates main contrib non-free

#sources respositories
deb-src http://http.kali.org/kali sana main non-free contrib
deb-src http://security.kali.org/kali-security sana/updates main contrib non-free

The output after installing some packagesapt-get update is :-
W: Failed to fetch http://http.kali.org/kali/dists/sana/InRelease

W: Failed to fetch http://security.kali.org/kali-security/dists/sana/updates/InRelease  Unable to find expected entry 'non-fre/source/Sources' in Release file (Wrong sources.list entry or malformed file)

W: Failed to fetch http://security.kali.org/kali-security/dists/sana/updates/main/source/Sources  Unable to connect to security.kali.org:http:

W: Failed to fetch http://security.kali.org/kali-security/dists/sana/updates/contrib/source/Sources  Unable to connect to security.kali.org:http:

W: Failed to fetch http://http.kali.org/kali/dists/sana/Release.gpg  Unable to connect to http.kali.org:http:

W: Some index files failed to download. They have been ignored, or old ones used instead.

Please post feedback if anything else is to be updated
Answer: I'm seeing the same thing today.  I was able to update one of our machines, earlier, but now it's mostly dead.
I know forums.kali.org is protected by Sucuri cloud proxy.  I can manually browse to most of http.kali.org, but occasionally get an error.  I wonder if the rest of the domain is also fronted by them.
As I type this, I'm watching a machine that just was able to get through apt-get update, but is timing out trying to fetch packages.
0% [Connecting to http.kali.org (18.104.22.168)]

Wish I could help more than confirm... it's not your machine.
Comment: tried now ,same thing hope it would get resolved soon
Comment: Yeah . it did update earlier .. while thanks for the confirmation again .. but need to find  any other way or source list ..
Comment: I tried it Today ..  and it worked ! .. i am happy though but suspicious of the situation at some time ..  Thanks for the help ..

Kali linux Source lists fails


Transform data while keeping the mean constant

Question: I am using an OLS regression to fit a model to some data. The estimated response is given by the usual
$$\mathbf{\hat{y}} = \mathbf{Py} = \mathbf{X}(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}$$
If the design matrix $\mathbf{X}$ contains a column of $1s$ for the intercept term, then each row (and each column) of the projection matrix $\mathbf{P}$ sums to $1$. This means that the mean of $\mathbf{\hat{y}}$ is equal to the true mean of $\mathbf{y}$, i.e.
$$\mathbf{1}^T\mathbf{\hat{y}} = \mathbf{1}^T\mathbf{Py} = \mathbf{1}^T\mathbf{y}$$
The issue I face is that $\mathbf{\hat{y}}$ may have some values falling outside some allowed region (say, negative values), so I would like to apply a transformation to $\mathbf{\hat{y}}$ such that the mean is maintained but the values are all positive.
Comment: When you face this issue, you should seriously contemplate using a different model.  If you "transform" $\hat y,$ then you risk losing every property of OLS for which it might have been indicated: it will not longer be unbiased among linear estimators; the p-values will be wrong; and you cannot interpret the coefficients in the standard way.
Comment: If you can  do weighted least squares (and re-calculate weights based on the fit to re-fit) then you could replicate (for example) GLMs and nonlinear least squares using linear least squares and so end up using more suitable models in spite of the restriction.
Comment: 1. You can do weighted LS using unweighted LS. 2. IRLS is used for much more than just heteroskedastic errors.See, for example, slide 14 [here](http://wwwf.imperial.ac.uk/~bm508/teaching/AppStats/Lecture5.pdf) which shows how to set up the equations in the case of GLMs. Full details are in many places (I think including the classic McCullagh & Nelder book n GLMs but I don't have it to hand right now).
Comment: @whuber Thank you for taking time to read my post and your comment. Unfortunately, I often face constraints imposed externally (such as the use of OLS in this case) which cannot be overcome so easily and I have to come up with (at times "crude") "fixes". This is one of them.
Comment: @Glen_b Thank you for your comment. OLS is all I have to work with at the moment, but I would be interested in exploring your suggestions further. As far as I know, iteratively reweighted least squares is used when the errors are heteroscedastic, but I might be confusing it. Would you be able to point me to a resource / example of applying the technique? Thank you
Comment: @Glen_b Looking at some resources, such as this entry on [Wikipedia]( https://en.wikipedia.org/wiki/Iteratively_reweighted_least_squares), I see that weights are given as $w_i(t) = |y_i-X_i\beta(t)|^{p-2}$ which for $p=2$ norm means that the weights are always kept at 1, so it doesn't seem to change anything. I must be missing something.
Comment: The main thing missing is the fact that WIkipedia isn't always an ideal reference. That article is written by someone coming from a particular point of view (looks to me like a non-statistician using it in one particular framework), someone ignorant of the very wide variety of applications of reweighted least squares. Indeed the intro to that article 7 years ago at least pointed at the broader landscape - some ass has removed all the pre-exiting parts that give any clue to its broader utility. I already pointed to something that shows how the reweighted least squares updates work in GLMs.
Comment: @Glen_b It seems that in order to use IRLS to find parameters that maximise log-likelihood in GLMs one has to use canonical parameter $\theta$ so that the log-likelihood can be expressed as a product containing $(y_i - \mu_i)$. Is that correct? What if I want to use different parameterisation? Can I still use IRLS to find the solution? In particular, what if I want to use the shape parameters $\alpha$ of gamma distribution as the varying parameters that depends on $\mathbf{X}$?
Comment: Correction to my comment above: the term $(y_i - \mu_i)$ is in the derivative of the log-likelihood w.r.t. $\beta$ (the parameters to be estimated), not in the log-likelihood itself.
Comment: When the shape parameter $\alpha$ of the gamma distribution is used as a dependent variable, I find that $\frac{\partial l_i}{\partial \beta_j}  = (\ln(\nu y_i) - \psi(\nu \mu_i)) \nu \frac{\partial \mu_i}{\partial \eta_i} x_{ij}$ and $E[\frac{\partial ^2 l_i}{\partial \beta_j \partial \beta_k}] = -\psi_1(\nu \mu_i) (\nu \frac{\partial \mu_i}{\partial \eta_i})^2 x_{ij} x_{ik}$ where \nu is the second "rate" parameter of the gamma distribution, and $\psi$ and $\psi_1$ are the digamma and trigamma functions. This doesn't seem to be "expressable" as a product of a weights matrix and design matrix
Answer: Let $Y_i^n$ be new $\hat Y_i$ value after transformation.
$$Y_i^n = \frac {\hat Y_i-\bar {\hat Y}}{\bar {\hat Y}- \min(\hat Y_1,...,\hat Y_n)}\bar{\hat Y} + \bar {\hat Y}$$
Then $Y_i^n \ge 0$ and $\bar Y^n = \bar {\hat Y}$ under the condition that $\bar {\hat Y} > 0$. Of course, if $\bar{\hat Y} \le 0 $, there is no solution.
Comment: What connection does your "$\bar Y$" have with the $\hat y$ of the question?
Comment: modified to reflect $\hat Y$.
Comment: Okay, thank you.  It makes one wonder what the resulting value would accomplish.  How would it be interpreted? What would be the properties of this estimator?  It's obviously at risk of being an extremely poor predictor!


How to maintain the page state when browser back button is clicked?

Question: I have a page with Client side paggination and Filtration. The page lists around 200-300 prouducts.
The page contains some filters like Category,Manufacturer and Weight.
Clicking upon any of the page number or filter, I am manupulating the page content on client side using Jquery.
Everything is working fine till this step. Now there is a usecase where I am facing problem.

Lets say a user comes to our product listing page and click on some of the filters and gets a list of products.
Now he clicks on a particular product , which redirects him to the product page to view the details of the product.
But now when the user clicks on the back button , the user gets the page with the intial state without any filter selected.

Is there any way user will get the page with the filters previously selected on clicking the back button?
Comment: store the state in a service, check for state when controller intitializes
Comment: You are talking about breadcrumbs which can be done in backend part of application, but also u can use localStorage. Provide html of your filters for further help.
Comment: what do you mean by "service" in this context? I would store the settings in cookies
Answer: You can use some of the following to store data across multiple pages.

Store data in cookies.
Store data in local storage.
Store data in the session on the server.
Make the data part of your URL (use hash or query string for the filter parameters). Note that changing query string causes page reload.

If using cookies, local storage, or hash, you'll need to add JavaScript code to your page that loads and applies the stored data on page load.
Comment: Lets say clicking upon a particular filter(Checkbox) I will add that value to the page url (Ex: page.cfm?categoryid=4,5) , and now I need to check these url variable on document.ready and trigger those filters through jquery? Am i correct?
Comment: Yep. But I'll move this option to the bottom of the list, because it causes page reload on each filter selection. If your application is purely client-based, I'd suggest using cookies or local storage.
Comment: Is it not possible to append filters to the url on filter selection with out refreshing the page.
Comment: It actually is. You'd just have to use the `hash` portion of the URL, not the query string.
Comment: My filtration is completely Client side . I do not need to make any ajax call on selecting a filter. I am just hiding the products that does not match filter criteria.Let say if i will store the filter value in cookie on selecting the filter ,On clicking back button I need to check those cookies on document.ready and manually trigger the filters ? Am I going in correct direction/?
Comment: You are. I'll add it to the answer.
Answer: There is a number of ways to do this:

If you are dealing with html5 history and a single-page application, then you are not reloading the page. But based on your question, I assume this is not what you are dealing with.
Store something in the URL. For an example of this, look at the filters on TotalHockey, e.g. http://www.totalhockey.com/Search.aspx?category_2=Sticks%2fComposite%20Sticks&chan_id=1&div_main_desc=Intermediate&category_1=Sticks so when you go backwards, the URL contains the entire state.
Use localstorage, if you have a browser that supports it.
use cookies with the $.cookie API
Store it on the session in the server.
Comment: @hon2a you and I must have crossed wires! Good to validate! I will upvote yours too!
Comment: Lets say clicking upon a particular filter(Checkbox) I will add that value to the page url (Ex: page.cfm?categoryid=4,5) , and now I need to check these url variable on document.ready and trigger those filters through jquery? Am i correct? Can it be done using jquery only or I need to use History API. I have no knowledge on history API.
Answer: You can store the Search Filter Data in session just after submitting on the filter input and on each ajax request (Loading your product listing), you can check the search filter inputs stored in the session and show the data according to them. If search session is empty then show whole listing.
You can also store the full ajax request URL (if GET method is used) in the session after searching the record and hit that particular URL again after coming back from product detail page.


How do I show multiple values from an entity reference?

Question: I have a content A that uses reference from users. Users, basically, have more than a content A.
I have view that shows users and adds a relationship from the field used in content A. I added a field to show the title of content A related to the user.
I can show it, but if users have more than one content a, it shows multiple rows for the same users, instead of showing a single row listing all the referenced content A.
How can I achieve this?
Answer: Under multiple field settings, chekmark display all values in the same row
Comment: i don't have that option in my field setting, so basically.. when i add the realtionship from content "a" field (which have reference to user) to view that list user.. it just create new row instead..

i event dont add field that inside content "a" which have relationship to user for my view...

so what i'm missing ?
Comment: this is the capture image --> https://i.ibb.co/8jCMhkZ/Screen-Shot-2019-11-13-at-14-06-46.png
Comment: @RenaldiYulvianda Add the reference field rather than title


Proving some two sequences is going to $0$

Question: Let $\{a_{n}\}$ and $\{b_{n}\}$ be two sequences of a real numbers. If $\lim_{n \to \infty} a_{n} = 0$, then prove  $$\lim_{n \to \infty} a_{n} \sin(b_{n}) = 0 $$
$|\lim_{n \to \infty} a_{n} \sin(b_{n})  - 0|  <= \epsilon$
un.. its kind of obvious $\sin(b_{n})$ is alternating and bounded
and finite * 0 = 0 .. but I'm not sure how to put this nicely
Comment: Relevant: http://math.stackexchange.com/questions/9649/if-b-n-is-a-bounded-sequence-and-lim-a-n-0-show-that-lima-nb-n-0
Answer: Do you know the squeeze theorem?
For any $n$,
$$
0 \le |a_n \sin b_n| = |a_n||\sin b_n| \le |a_n| \cdot 1 = |a_n|
$$
$$
0 \le |a_n \sin b_n| \le |a_n|
$$
Now what happens do this inequality when you take the limit as $n \to \infty$?
Comment: Neat, simple and short. +1


How can I get the page title?

Question: drupal_get_title() has been removed from Drupal 8. How I can get the page title in Drupal 8?
Comment: This answer here below on this page works in Drupal 9.0.7: https://drupal.stackexchange.com/a/250205/1082
Answer: If you check the change records you should find the following: drupal_set_title() and drupal_get_title() were removed.
You'll have to read the change notice for all of the info, but here is a starting point:

drupal_get_title()
As titles on routes now can be set on various ways (see above),
  drupal_get_title() has been removed. In its place you should call the
  title_resolver service.
Drupal 7
<?php
  $title = drupal_get_title();
?>

Drupal 8
<?php
  $request = \Drupal::request();
  if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
    $title = \Drupal::service('title_resolver')->getTitle($request, $route);
  }
?>

Comment: The accepted response is out of date: see elsewhere in this thread for CurrentRouteMatch.
Answer: As per batch_test.module (D8.4) the following should work just fine:

$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
$title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());

For more info see public function TitleResolver::getTitle.
Comment: @therobyouknow – You are welcome my friend! BTW I noticed you tagged the question with and additional version tag. Maybe read https://drupal.meta.stackexchange.com/q/3640/15055 as an introduction regarding version tag usage and please try to avoid multiple version tags on the same post. Rather use none.
Comment: +1 Confirming this works with Drupal 9 (9.0.7), with node-based pages at least.  I'd expect it would work with non-node pages too but I haven't tried that yet.
Comment: FYI - This approach doesn't work on node edit pages.
Answer: Note
The accepted response for this question is out of date: See Request parameters, route name, and route object should be accessed via RouteMatch.
You should use the current_route_match service to retrieve the current route, and then read or modify its parameters. This wraps any Symfony etc. magic.
In procedural code, you would use code similar to the following one.
$route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject();
$title = $route->getDefault('_title');

In object-oriented code, you should use dependency injection and inject the service.
Comment: This bypasses the TitleResolver so doesn't handle all situations. https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Controller%21TitleResolver.php/function/TitleResolver%3A%3AgetTitle/8.7.x
Comment: What to do if there is no `$route->getDefault('_title');` but only a `$route->getDefault('_title_callback');`?
Comment: For example on http://example.com/user/1
Comment: Using this solution will get the untranslated title.
Answer: Drupal 8
$request = \Drupal::request();
  // Assuming the Request is $request.
  if ($request->attributes->has('_title')) {
    return $request->attributes->get('_title');
  }

Comment: I didn't mark this response down, but it looks like the `->get('_MAGIC')` method is unfortunately deprecated and mostly removed from D8: https://www.drupal.org/node/2124749
Answer: In drupal 8 you can use $node->getTitle().
Comment: The issue with this answer is that the page you're on isn't necessarily a node.

How can I get the page title?


Mastering ajax data loading

Question: It's not that I'm not familiar with the concept but I'm wondering about what is the best approach when you are creating applications supporting ajax userexperience.
I mostly use ajax with jQuery but also when I want to load information without a pagerefresh. As you will probably know, the XmlHttp Object and the ResponseText provides a nice and easy way to execute a script in the back and display the results e.g. a div.
A bit of a downside of this approach is that its hard to see the actual generated sourcecode. I often take a look at the sourcecode to see if the expected parameters are correctly provided to e.g. formelements.
So I'm curious, what is your approach for creating ajax-data-load functionality? Is it just by the ResponseText property?
Answer: Having manually crafted XmlHttpRequest objects for a while, I now use jQuery for all my AJAX-ified stuff.  It gives you much better control while coding less.
If you use Firefox, get yourself the Web Developer Toolbar.  It has a cool feature called "view generated source code" which generates the HTML code that the browser knows about in the current document as it stands, so it includes the HTML sent back by your AJAX requests.
Also I make it a rule to always tell the user you're loading something rather than relying on the browser to tell them (like Gmail's "Loading" text in the corner for instance.)
Comment: @Ben Fransen - Also grab the FireBug extension, it's tremendously helpful for this area.
Comment: @Ben - I assume by "page data" you mean loading page content via AJAX like you appear to be doing using XmlHttpRequest?  If so then yes - check out the AJAX functionality: http://api.jquery.com/category/ajax/  I used it once and never looked back :)
Comment: Does jQuery also provide you with functionality to load page data? I have the webdeveloper toolbar.. I only used it for CSS and javascript errorchecking ;) I'll check out that feature too! And about your rule: I totally agree with you, it's actually one of my mainreasons for starting learning ajax.
Comment: @Nick, I have that one installed too. As well as YSlow, it's a nice tool to check the performance of the websites you're building.

@Andy, I'm gonna take a look at it. You're right about me using the XmlHttpRequest object for that purpose.

Mastering ajax data loading


how can i show the time availiblity to the user who want to book appoinment

Question: hi i have a schedule for each provider in data base mysql.for a week schedule like open time on monday to sunday or closing time of monday to sunday.now i want to show available time having a provider to display according to service choose by customer.i have the specfic service and businessID also.how can i divide the sloat for each day for provider.And the opening and closing time are saved in Db in Varchar format..please help if some one can. i will be greatfull to you .or atleast give some idea how to do this
Comment: And on your question asking skills. Here's an [nice article](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx) I recommend you reading.
Answer:
the opening and closing time are saved
  in Db in Varchar format

That's a big big big mistake. By storing times/dates in anything other than the natural date and datetime types in MySQL, you completely lose the ability to use MySQL's native date/time handling facilities, and will have to do all processing in your client-side code, or write hideously ugly queries to do so in MySQL. Either way, you'll be subjecting yourself to unecessary grief by using varchar instead of date or datetime


Totally decomposable vectors as quotient of monomorphisms by SL

Question: Consider vector spaces $V$ and $W$ of dimensions $m$ and $n\geq m$, respectively.
Both the special linear group $\operatorname{SL}(V)$ as well as the general linear group $\operatorname{GL}(V)$ act smoothly and freely from the right on the smooth manifold of monomorphism $\operatorname{Mono}(V;W)$ by precomposition. Denote the space of totally decomposable $m$-vectors in $W$ by $\bigwedge^m_sW$.
Fix a basis $e_1,\ldots,e_m$ of $V$ and define the surjective map
$$\pi\colon \operatorname{Mono}(V;W)\to \bigwedge\nolimits^m_sW\setminus\{0\}, A\mapsto Ae_1 \wedge \cdots \wedge Ae_m.$$
I can show that the map
$\Phi\colon \operatorname{Mono}(V;W)/\operatorname{SL}(V) \to \bigwedge^m_sW\setminus\{0\}$ defined by $\Phi([A]_{\operatorname{SL}}) = \pi(A)$ is well-defined and bijective.
Analoguously, I can show that $\Psi\colon \operatorname{Mono}(V;W)/\operatorname{GL}(V) \to \bigwedge^m_sW\setminus\{0\}$ defined by $\Phi([A]_{\operatorname{GL}}) = \{t \pi(A)\colon t\in\mathbb{R}^{\times}\}$ is well-defined and bijective.
Hence the following diagram commutes:

My main issue is whether $\Phi$ and $\Psi$ are diffeomorphisms. To even start to show this each space should be a smooth manifold.
I know that by the Plücker embedding $\mathbb{P}(\bigwedge^m_sW\setminus\{0\})$ is the Grassmannian and thus a compact smooth manifold of dimension $m(n-m)$.
The quotient on the bottom right also should have dimension $nm-m^2=m(n-m)$ but I'm unsure how to show that it's a smooth manifold. If $\operatorname{GL}$ acted properly then the quotient manifold theorem would be my friend. Is this the case?
The quotient on the middle right should have dimension $nm-(m^2-1)=m(n-m)+1$. The totally decomposable $m$-vectors are a projective variety of "non-projective" dimension $m(n-m)+1$. So these arguments at least indicate to me, thankfully, that my bijectivity proofs are not bogus.
I'm unsure if the spaces on the middle level are actually manifolds. On this level, also, the map $\pi$ does depend on the chosen basis whereas (an analogous map) on the bottom level does not. Maybe another indication that the bottom-level compactification makes things "better"?
What "regularity" of $\Phi$ could I even expect? I want to show that the totally decomposable vectors are isomorphic to the middle right quotient in an appropriate category.
Answer: Let $U$ be the tautological (rank-$m$) vector bundle over the Grassmannian $\mathrm{Gr}(m,W)$ and let
$$
P := \mathrm{Iso}(V,U)
$$
be the corresponding principal $\mathrm{GL}(V)$-bundle over $\mathrm{Gr}(m,W)$. Then
$$
P \cong \mathrm{Mono}(V;W).
$$
Indeed, the map from left to right is given by taking an isomorphism $V \to U$ to the composition $V \to U \to W$, where the second arrow is the tautological embedding, and it is easy to see this map is an isomorphism and, moreover, it is $\mathrm{GL}(V)$-equivariant. Taking quotients by $\mathrm{GL}(V)$ and $\mathrm{SL}(V)$, you can obtain the two isomorphism you are interested in.
Comment: 1. So $U$ is the bundle whose fibre at a point $E\in\operatorname{Gr}(m,W)$ is just $E$ itself and I have to interpret $P$ as the bundle whose fiber at $E\in\operatorname{Gr}(m,W)$ is $\operatorname{Iso}(V;E)$. Is that correct?
2. As a principle $\operatorname{GL}(V)$-bundle over $\operatorname{Gr}(m,W)$ its quotient by $\operatorname{GL}(V)$ is simply $\operatorname{Gr}(m,W)$. Why is its quotient by $\operatorname{SL}(V)$ the set of totally decomposable $m$-vectors?
Comment: 3. My main issue actually was which regularity the quotients and the isomorphisms have. The isomorphisms on the quotient by $\operatorname{GL}(V)$ will be a diffeo by your answer. What about the regularity of the isomorphism on the quotient by $\operatorname{SL}(V)$?
Comment: 1 is OK. For 2 choose a non-zero element $\delta \in \wedge^mV$ and consider the map $P \to \wedge W$ that takes an isomorphism $V \to E$, raises it to the $m$-th wedge power, and applies to $\delta$ (thus obtaining an element of $\wedge^mE \subset \wedge^mW$). It is equivariant for the action of $\mathrm{SL}(V)$ (the stabilizer of $\delta$) and induces an isomorphism of the quotient by it with the set of totally decomposable vectors. As for 3 --- the point of this construction is that instead of quotients you study principle bundles, for which there question of regularity is completely clear.
Comment: So the map $P\to \wedge^m W$ does exactly the same as the map $\pi$ in my construction above. Could you elaborate on the question of regularity a bit further; since it is, unfortunately, still unclear to me? The principal $\mathrm{GL}(V)$-bundle $P\to \mathrm{Gr}(m,W)$ is a bundle over a smooth manifold, the principal $\mathrm{SL}(V)$-bundle $P\to \wedge^m_sW$ is a bundle over an algebraic variety. In which regularity category is this isomorphism on the base space/$\mathrm{SL}(V)$-quotient $\wedge^m_sW$ described above? Maybe I'm missing something very trivial?
Comment: The action of $\mathrm{SL}(V)$ on $P$ is free, hence the quotient is smooth and isomorphic to the variety of decomposable vectors.


Accessing Azure File Share from Windows Web App

Question: I have a Windows Azure Web App that uploads files to a directory called 'CallRecordings' in the website root.
I wish to use a virtual directory pointing to an Azure file share. I have mounted the file share from the Azure portal and it appears at D:\mounts\callrecordings.
My question is how do I map this to a virtual directory so that the App cab use it? The portal only allows \sites mappings.
My test app is running on the F1 tier, should I use a different tier? I'm hoping to do this using configuration so that I don't have to change the App.
Any advice is gratefully appreciated.
Answer: Did you figure this out? I'm trying the exact same option, but adding a virtual path outside site\ seems impossible
Comment: I've got this on the back burner but it's still an issue
Comment: This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://serverfault.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://serverfault.com/help/whats-reputation), you can also [add a bounty](https://serverfault.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/523329)


What are some simplifications I can make in order to parse LaTeX strings in category theory, in a useful way?

Question: Here's a screenshot of a PyQt5 app I'm making.  It renders LaTeX by calling latex and dvipng from a MikTeK installation.

I want to make this diagram tool that acts as a notebook and perhaps diagram chasing tool.
There's a problem though.  How does the user specify the math symbols?  Clearly the set of possible strings from a category theory book is much different from general math.
The app will have to do at least some category theory in order to be a tool you'd want to use.  However representing all possible objects that would occur in a book seems daunting and error-prone.
I was thinking, maybe a <code>\cdot</code> could always mean that's where some morphism or functor composes, so in order to take the functor of a diagram, it's simple text replacement processing.    That is an example of a simplifying assumption.
Please give me some ideas of what I could do as general mathematical language processing is way too difficult.
Answer: Perhaps you could work with the macros in the 'diagrams' package collection? Either have the user enter the relevant macros directly, or create an interface on top of those macros.
Comment: I have no trouble drawing the diagrams though.
Comment: Okay, so I must have misunderstood what problem you're trying to solve; I was responding to you asking "How does the user specify the math symbols?" If having the user enter existing LaTeX macros (either directly or through another interface layer) does not solve the problem, could you explain why that is?
Comment: you are right that was misleading.  The user draws the diagram like in paint, so they only need to specify the node and arrow labels.  Parsing is very difficult to me, so was wondering if there are some simplifications.  Also, how would we do diagram chasing if we oversimplified it, so there's a trade off.


Checking the network connectivity stats for slow file transfer using Robocopy

Question: I am trying to transfer a 6.5 GB file between two Windows Server 2003 machines on our network. Using Robocopy //sourceserver1 //destinationserver2 filname /z is making the file transfer crawl and after three hours of initiating the request the file has only progressed to 26%. I am aware that the '/z' prompt slows down the transfer for Robocopy, but the lag here seems excessive.
I would like to diagnose what is going on in the network and try to figure out possible bottlenecks. Can somebody suggest where I should start with my quest or if I should have transferred the file in an alternative fashion?
Thanks
Comment: Old question, but it seems that `/Z` really kills your speed due to progress header updates.  After each chunk is written it re-writes the progress header on the file.  Restarts are great in some circumstances but they are also very slow.
Answer: Unless you have something like netflow, you might struggle to see whats taking up your bandwidth.
You can use a /ipg:n switch with your robocopy command to try and throttle the bandwidth back a bit, i have used that before over slow links.
You could try using ftp to move the file instead, install an ftp server where you want to copy the file to, and ftp to it from your source server. Using filezilla or similar you should be able to run it in restartable mode, and also see how much bandwidth it is taking up.
I have struggled sometimes copying large files in the past so i know how you feel!
Comment: Thanks for the netflow tip. Are there some working examples on using netflow to trackdown the pitfalls in the network infrastructure. The /ipg:n switch might also be helpful. Thanks
Comment: netflow is quite an in depth application, but it will show you what clients/applications/protocols are taking up your bandwidth


Is eslint pre-commit hook in CRA bootstrapped application required?

Question: My react application is bootstrapped using CRA(react-scripts v4.0.0) and I want to add a code formatter (prettier to be specific) that must format the code before committing the changes to my git repo.
I know that eslint is a dependency of react-scripts and linting is done using 'eslint-webpack-plugin'(default enabled) inside the webpack configuration. Snippet from CRA github | React Scripts webpack config
Now, if I want to extend my original eslint-config I can install/write a new eslint-config like 'eslint-config-prettier' and add it to my extends key inside eslint-config. And this would do the job.
Assuming, this updates the current eslint-config without installing any extra dependencies and as normal, would lint the application code every time (with updated config) when the code is changed. And would throw compilation errors as it should.
I have seen multiple tutorials over web installing eslint again as a dev dependency and then using it inside pre-commit hooks for fixing linting issues and then running the code formatters like prettier sequentially after that. Is installing and running eslint --fix in a pre-commit hook necessary at all? or is it redundant?
Comment: Preventing code commits on unfixable lint errors is a viable use case.
Comment: It's not necessary. It just ensures that any code that is committed runs through the linter. If you don't care about this, don't worry about hooking pre-commit for this purpose.
Answer: Even if CRA based react application halts the compilation on lint error, it does not strictly prevent the user from committing the bad code.
Adding the eslint --fix pre-commit hook halts the user's commit if there is an unfixable lint error present in staged files or the whole project (depending on the command, file glob pattern provided) thus prevents accidental push of bad code.
Although, installing eslint again in case of CRA is redundant as it is already present as a dependency for react-scripts.


How is PCA applied to new data?

Question: I understand the basic intuition behind PCA: reducing the dimensionality of data by finding the eigenvectors along which there is most variance in the data, and projecting the data along these eigenvectors (the principal components).
What I don't understand is the following:

How are the eigenvectors found? A standard eigenvector equation is given by $Av=\lambda v$, where $\lambda$ and $v$ are the eigenvalues and eigenvectors respectively. So what is the $A$ matrix - the data itself, or the covariance matrix of the data... or something else? (If the data matrix isnt square then this equation doesn't hold.)
Once PCA has been performed / trained on a data set, can it be applied to reduce the dimensionality of new unseen data? For this to be true, I suppose a mapping would need to be output by PCA, and this mapping could be applied to the new data, say in the form of matrix multiplication.

What are the outputs of PCA?
How are the outputs applied to new data, if at all?
Comment: Q1: $A$ is the covariance matrix. Q2: you multiply your new data with $v$.
Comment: Well if you want 2 PCs, then you take two eigenvectors corresponding to the two largest eigenvalues, and then multiply with those. Usually you have them stacked in a matrix V as columns, and can multiply your data directly with XV.
Comment: Aren't there multiple $v$'s? How would I multiply my new data by $v$ - which $v$?
Comment: https://stats.stackexchange.com/questions/210666/rotating-a-new-matrix-into-the-same-basis-as-another-using-svd
Answer: I will answer each question:

$A$ is indeed the covariance matrix (so $X^TX$ assuming $X$ is standardized)
The output of PCA is 3 things: the vector of column means $\mu$ of $X$, the vector of column stddevs $\sigma$ of $X$ and the rotation matrix $R = [v_1 ... v_p]$. Therefore, for a new sample row $x_0^T$, to compute its projection onto Principal Component Space, you have to standardize and rotate, that is $((x_0 - \mu) / \sigma)^T R$, which will yield a row vector with $x_0$ in PC coordinates. Please note that here I'm dividing by $\sigma$ elementwise.

How is PCA applied to new data?


ISA Server Publishing Rule to Allow Connections From a Particular WAN IP/Subnet

Question: I am looking to publish access to a particular server behind a Microsoft ISA server and limit access to a particular Public Server range. For instance, I only want to allow RDP access to the exchange server behind the ISA from the public IP address of my office. I'm pretty familiar with ISA server publishing rules, just having difficulty figuring out how to constrict the access to a particular IP range.
Comment: Which version of ISA?
Comment: Forefront TMG 6.0.6417.100
Answer: Go to the Toolbox in the ISA 2006 Manager.  Expand Network Objects, right click on Address Ranges.  Enter in your IP range. You may have to create multiple Address Ranges.  When you create a rule in the Firewall Policy, uses these Address Ranges on the From tab.  You can also add multiple items on this tab.


Exclude file directory, by assembly plugin MAVEN

Question: I would be pleasant, if you could help me to figure out this problem.
When I build my project to .zip with maven <assembly> plugin.
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

<id>dev</id>
<formats>
    <format>zip</format>
</formats>

<fileSets>

    <fileSet>
        <directory>${basedir}</directory>
        <includes>
            <include>/environments/dev/**/*.sh</include>
            <include>/environments/dev/**/*.jil</include>
            <include>/environments/dev/**/*.properties</include>
            <include>/environments/dev/**/log4j2.xml</include>
        </includes>
    </fileSet>

</fileSets>

<dependencySets>
    <dependencySet>
        <outputDirectory>/lib</outputDirectory>
        <useProjectArtifact>true</useProjectArtifact>
        <scope>runtime</scope>
    </dependencySet>
</dependencySets>

I receive the following file structure:
build.zip----> environments__
                            |_DEV_
                                  |_bin
                                  |_properties
                                  |_....

My goal is:
To get the following file structure after maven build:
 build.zip---->
                                  |_bin
                                  |_properties
                                  |_....

Without environments and DEV folders.
I've read in the maven documentation (http://maven.apache.org/plugins/maven-assembly-plugin/advanced-descriptor-topics.html) that we can exclude some file directories.
This way:
<assembly>
    ....

    <fileSets>
        <fileSet>
            <directory>${basedir}</directory>
            <includes>
              ....
            </includes>
            <excludes>
                <exclude>/environments/dev</exclude>
            </excludes>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

But it doesn't help. I still have previous file structure.
Answer: You want to set the root of your directory to /environments/dev
<fileSet>
    <directory>${basedir}/environments/dev</directory>
    <outputDirectory>/</outputDirectory>
    <filtered>false</filtered>
</fileSet>

Answer: From the documentation of maven-assembly-plugin (maven-assembly-plugin) I can see that the <fileSets> tag does not provide us with the option of changing the path of an included resource. Instead we can use the <file> tag which gives us this flexibility.
For example, the below configuration will include file1.jar and run.bat in the root folder of the extracted zip file, skipping their original paths.
<assembly>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <files>
    <file>
      <source>target/file1.jar</source>
      <destName>file1.jar</destName>
    </file>
    <file>
      <source>src/main/resources/run.bat</source>
      <destName>run.bat</destName>
    </file>
  </files>
</assembly>


VLAN's between DELL 3548 and DELL 6248 (not using trunk)

Question: I have a 6248 switch on one floor connected to 3 other floors in the building, all seems to be working but on one floor I would like to have a separate VLAN for a WIFI Access Point, and also boardroom access etc.
I was going to post pictures but don't have enough reputation yet, I am using the GUI to setup.  Basically under VLAN Membership on both switches I have create a VLAN called WIFI using VLAN ID of 4 on both, all the ports in the WIFI VLAN show U.
There 2 separate cables between the floor and I want the traffic to remain separate so I don't want to use trunking.
I assumed if I kept it all separate the VLAN setup should be straight forward but the traffic will not flow between the two switches, if I replace the 3548 with a non-managed switch it all works.
What am I missing here?
Thanks
Answer: Well you can't have a VLAN that spans two or more switches without trunking the switches.
An unmanaged switch works because it sends all traffic between the switches, regardless of the VLAN, which means that all of the traffic on the unmanaged switch is co-mingled, again regardless of VLAN.
The VLAN traffic is already "separated" by virtue of being in separate VLAN's, which is the entire point of using VLAN's. I'm not really understanding what you're trying to achieve with your current set up.
Comment: I have a cable for each VLAN between the two switches so would it not only be sending all the traffic over those cables regardless, a similar question was posted here [link]http://serverfault.com/questions/403458/how-to-interconnect-2-switches-cisco-dell-with-trunks?rq=1
Answer: You're missing that your switches are all VLAN aware and thus throw away packets that have VLAN tags that are not set up.
Comment: I figured since I was using a separate cable between the switches for each VLAN all the traffic for that VLAN would pass through and then go to all the ports on the other switch.  At what point is this failing? On the 6248 I have a router handing out IP's and I can connect a device to another of the VLAN ports on the 6248 and it gets an IP, but not on a corresponding VLAN port on the 3548.  So are you saying that even though they are connected via separate cable, that when the 6248 sends a packet upstairs it is not tagged properly so the 3548, just ignores?


How to design 3x3 buttons in auto layout support all iPhone devices using Storyboard

Question: I need one help to design 3x3 buttons in a view controller. It fits all iPhone devices like 4,5,6 and 6+. Any one guide me how to design it.
Comment: kindly attach some visual clue, for us to better understand your problem
Comment: Use `UICollectionViewController `
Comment: Clue means three rows three columns of buttons I want to display in a tab view controller.  these three rows and three columns are fit all screen sizes not stretched.
Answer: Use autolayout in your xib or storyboard. Put 9 buttons lets say

123
456
789

Start to make alignments :

123 -> align top views to superview.
789 -> align bottom views to superview.
147 -> align left views to superview.
369 -> align right views to superview.
And select all buttons and set to same width and same height then your are done!


How to detect when a socket can be closed?

Question: I am printing to a thermal ticket printer which has no standard drivers. Traditionally our business process has been to produce a batch file of print items and to deliver that through an open socket to the printer. When the program has finished processing and prior to tidying up by shutting the socket I blocked the process by popping up a MessageBox with a warning to the user not to press OK until printing had finished: the next line of code then forcibly closed and cleared away the socket.
It was a bit of a sticky tape and string solution but it served its purpose.
I've recently overhauled the printing process using WCF Webservices and LINQ data management. As a result the batch files have become less necessary (they're still useful in the case of printing or reprinting for customers because the batchfile forms a sort of "receipt" to say that the physical ticket exists). But the old "stuff up a messagebox" solution to telling people when it's safe to close the socket looks unacceptably creaky in this new program.
So I want to have it just finish using the socket, exit gracefully and then pop up the box to say that printing is finished. Having a quick look around at possible solutions to this problem I have come across:

The idea of using a separate thread for the printing process
The idea of asynchronous sockets
A method called Socket.Shutdown

What I'd really like is an overview of advantages/disadvantages of these three approaches.
Our usage scenario is that we seldom need use of a "queue" as the ticket printer isn't used anywhere near as heavily as a regular office printer. The printing of a batch of customer tickets or of 300 tickets for sale over the counter at a venue is still an event there aren't really disputes about who wants to do what and when. Even at one of our client sites where there are enough people that usage of the printer could cause conflicts the printer is usually closely monitored as it does its work.
This is, of course, because the minute a ticket comes out of the printer it has a monetary worth. As soon as the tickets are printed they tend to be taken away to be securely stored.
So bearing in mind this business process I want something unfussy that needn't worry overmuch about simultaneous requests.
EDIT: I did a bunch of research and tried different things on this. In this particular scenario the fact that the print socket blocks the UI thread is not a problem. People don't want to continue using the UI whilst the valuable stream of tickets streams out of the printer. But after looking at the way ASync requests work it seemed more logical given the most likely usage scenario to open the socket when the print options page opened and close it as it exited. The Async tutorials I did indicated that this is pretty much how things would work anyway.
Answer:
The idea of using a separate thread for the printing process:
In this case you're still using sockets synchronously i presume. Which means you're still blocking the thread when awaiting a response. Which isn't a problem in a seperate thread, but performance wise it still isn't the way to go.
The idea of asynchronous sockets:
Is the standard way to use a socket when you don't want to block your current thread. This doesn't do any blocking operations which means better performance. The disadvantage is maybe that synchronous code looks a bit more clear and is easier to use. I would go with this one.
A method called Socket.Shutdown: This isn't a solution to your problem. It is a way to disable sending and receiving on a socket. It is best practice to shutdown the socket before closing it.
For example in C#:
socket.Shutdown(SocketShutdown.Both);
socket.Close();

Remember though when closing your application that you should wait until all backgound threads have been finished before actually letting the application close.
Comment: Weirdly regarding three a lot of people were citing use of Socket.Shutdown to ensure all data had finished being sent before closing the socket. To my mind it didn't seem to provide any such assurance. So I'm currently giving it a go. Seeing as even if it works it seems to be a byproduct I may reconsider using it if it does work.
Comment: You're right, I actually do remember that it does that... but it's also a blocking operation. You're probably doing it in the UI thread which means the UI will be unresponsive until it is ready. Which isn't user friendly.
Comment: There's not much difference technically between the first two solutions.  The asynchronous socket will call the AsyncCallback delegate on a ThreadPool thread when the operation is complete.  Either way, you'd have to signal the UI thread somehow that the socket operation is finished.  Most people would recommend the asynchonous socket approach, and there may be a technical reason for doing so.  However, creating a Thread object with a ThreadStart delegate that performs the socket operation synchronously seems to be about the same to me.  You say tomato, I say bowling shoes...
Comment: Sure it has the same effect for the user, but it doesn't for the program or the developer. It's an inefficient way for doing the same thing. In my mind when you have multiple possibilities you should choose the best one. Also ThreadStart just starts up a new thread. You should atleast use the threadpool unless you have very good reasons for doing otherwise.


Using mathjax in an updating sequence in javascript

Question: I have four dynamic variables in my program,
xzero, xone, xtwo, xthree

That represent the coefficients of a cubic function. I want to show the actual equation in the HTML, in the following format:
xthree*x^3+xtwo*x^2+xone*x+xzero=f(x)

but it needs to be able to update with the variables, so I had originally thought that I would do a
document.getElementById("demo").innerHTML = (new equation);

But it seems to me that either mathJax doesn't work in javascript, or that I'm not doing it right. The only alternative solution that I could think of is to make four individual div tags per variable, and update them all, but this seems unnecessarily clunky. Could I get some pointers either way as to how I would fix this?
Answer: MathJax automagically renders the document when it is loaded, but not later. Try explicitly requesting re-render:
document.getElementById("demo").innerHTML = "...";
MathJax.Hub.Queue(["Typeset", MathJax.Hub, 'demo']);

EDIT: After some thinking, I figured you could ask for the render in one element, keep it hidden, then copy the finished markup into another, to prevent flicker:

var mathDiv = document.getElementById('math');
var displayDiv = document.getElementById('display');

MathJax.Hub.Queue(["Typeset",MathJax.Hub,"math"]);
MathJax.Hub.Queue(function() {
  var math = MathJax.Hub.getAllJax("MathDiv")[0];
  var i = 1;
  setInterval(function() {
    MathJax.Hub.Queue(["Text", math, "\\int_0^{" + i + "} x dx"]);
    MathJax.Hub.Queue(function() {
      displayDiv.innerHTML = mathDiv.innerHTML;
    });
    i++;
  }, 1000);
});
#math {
  display: none
}

$$$$

Comment: That seemed to work, but for a brief second, when I update it the equation seems to show as $...$ before rendering. This is causing my page to shudder, so is there any way to fix this?
Comment: No, Update doesn't seem to do anything to rerender it, unfortunately
Comment: Oh. Well, after a bit more thinking, I think this should work then.
Comment: Note from the future: cdn.mathjax.org is nearing its end-of-life, check https://www.mathjax.org/cdn-shutting-down/ for migration tips.


C# developers learning Java, what are the biggest differences one may overlook?

Question: For c# developers that are staring out to learn Java, are there any big underlying differences between the two languages that should be pointed out?
Maybe some people may assume things to be the same, but there are some import aspects that shouldn't be overlooked? (or you can really screw up!)
Maybe in terms of OOP constructs, the way GC works, references, deployment related, etc.
Comment: I think at least one of these might be a duplicate: http://stackoverflow.com/questions/614310/good-java-resources-for-c-developers
http://stackoverflow.com/questions/870342/java-for-a-c-developer-closed
http://stackoverflow.com/questions/90578/best-way-to-really-grok-java-for-a-c-guy
http://stackoverflow.com/questions/72719/good-book-for-a-c-developer-going-over-to-java
http://stackoverflow.com/questions/614310/good-java-resources-for-c-developers
http://stackoverflow.com/questions/323015/best-way-to-learn-java-closed
Comment: Also http://stackoverflow.com/questions/295224/what-are-major-differences-between-c-and-java.
Comment: Also likely to be overlooked are our own "Hidden Features of C#" http://stackoverflow.com/questions/9033/hidden-features-of-c
Answer: A few gotchas off the top of my head:

Java doesn't have custom value types (structs) so don't bother looking for them
Java enums are very different to the "named numbers" approach of C#; they're more OO. They can be used to great effect, if you're careful.
byte is signed in Java (unfortunately)
In C#, instance variable initializers run before the base class constructor does; in Java they run after it does (i.e. just before the constructor body in "this" class)
In C# methods are sealed by default. In Java they're virtual by default.
The default access modifier in C# is always "the most restrictive access available in the current context"; in Java it's "package" access. (It's worth reading up on the particular access modifiers in Java.)
Nested types in Java and C# work somewhat differently; in particular they have different access restrictions, and unless you declare the nested type to be static it will have an implicit reference to an instance of the containing class.
Comment: @cwap: In C#, the execution order is "instance variable initializers, base class constructor, constructor body". In Java it's "superclass constructor, instance variable initializers, constructor body". (Instance variable initializers are what you get when you assign a value to an instance variable at the point of declaration.)
Comment: +1 This is a great list for starters.
Comment: @Jon Skeet: +1, And you would probably miss Lambda and LINQ until Java 7?
Comment: "In C#, instance variable initializers run before the base class constructor does; in Java they run after it does (i.e. just before the constructor body in "this" class)" -- Eeer, could you elaborate this? Not sure I really understand what you're saying :)
Comment: @cwap: Yes, that's what I mean - the latter is an *object* initializer.
Comment: why no one speak about the absence of passing the parameter by reference in java so if you want to return multiple values you should create a type for that.
also java have a labeled break and labeled continue which not found in C#.
Java didn't have properties you should get and set attributes
Comment: @Kb: Yup, I miss it regularly :(
Comment: Not only bytes but all integer types are signed
Comment: When you're talking about "instance variable initializers", you mean class A { int myInt = 4 /* <-- Here? */ } ?
For a moment I thought you meant the classA A = new A(){myInt = 2}; which I didnt really believe :)
Comment: I've never had to use an Enum, maybe I'm not doing it right...
Answer: here is a very comprehensive comparison of the 2 languages:
http://www.25hoursaday.com/CsharpVsJava.html
Added: http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp
Answer: I am surprised that no one has mentioned properties, something quite fundamental in C# but absent in Java. C# 3 and above has automatically implemented properties as well. In Java you have to use GetX/SetX type methods.
Another obvious difference is LINQ and lambda expressions in C# 3 absent in Java.
There are a few other simple but useful things missing from Java like verbatim strings (@""), operator overloading, iterators using yield and pre processor are missing in Java as well.
One of my personal favourites in C# is that namespace names don't have to follow the physical directory structure. I really like this flexibility.
Answer: There are a lot of differences, but these come to mind for me:

Lack of operator overloading in Java. Watch your instance.Equals(instance2) versus instance == instance2 (especially w/strings).
Get used to interfaces NOT being prefixed with an I. Often you see namespaces or classes suffixed with Impl instead.
Double checked locking doesn't work because of the Java memory model.
You can import static methods without prefixing them with the class name, which is very useful in certain cases (DSLs).
Switch statements in Java don't require a default, and you can't use strings as case labels (IIRC).
Java generics will anger you. Java generics don't exist at runtime (at least in 1.5), they're a compiler trick, which causes problems if you want to do reflection on the generic types.
Comment: Last I checked, strings did NOT overload ==, and I haven't heard of it being added for Java 7 either. They do, however, overload + for string concatenation.
Comment: Yes, comparing strings with == is a very common pitfall for newbies. `.equals` is what you have to use.
Comment: "Switch statements in Java don't require a default" - nor does C#.
Comment: Also, I don't understand the comment about static methods. Don't pretty much all languages allow static methods or the equivalent?
Comment: I nearly upvoted this, but I disagree about generics.  I like the type erasure.
Comment: @finnw, you *like* type erasure!?!?  Are you high?  Even the designers of Java understood that it's a sacrifice for forwards-compatibility of the language for old JVMs.
Comment: Double-checked locking does work since Java 5 (if implemented correctly).
Answer: .NET has reified generics; Java has erased generics.
The difference is this: if you have an ArrayList<String> object, in .NET, you can tell (at runtime) that the object has type ArrayList<String>, whereas in Java, at runtime, the object is of type ArrayList; the String part is lost. If you put in non-String objects into the ArrayList, the system can't enforce that, and you'll only know about it after you try to extract the item out, and the cast fails.
Comment: ...although it's worth adding that you cannot put non-`String` objects in that list without making an unchecked cast, *somewhere*, and the compiler will correctly warn you at that point that the compiler can no longer check the generic bounds.  So long as your list is *always* stored in a variable of `List`, attempts to insert a non-String object into it will not compile.
Comment: @Andrzej, @sleske: I'm only talking about runtime. Say you get the object via reflection, and invoke its `add` method (again, via reflection). Will the system reject a non-`String` object straight away, or only when casting the result of `get`?
Comment: "non-String objects into the ArrayList, the system can't enforce that": Note that the system *does* enforce it at compile-time. You can however circumvent this by casting (or not using generics); then the runtime  check will catch it, but only when extracting.
Comment: @sleske: But IIRC you only need an implicit cast to circumvent the type check.
Comment: @Judah: I rolled back your change (yes, a year after the fact), because you cannot have objects of type `List`. You can have objects of types descending from `List`, like `ArrayList` and `LinkedList`. (It's important to use `List` instead of `ArrayList` when passing them around, but it doesn't change the fact that you cannot say `new List()`.)
Comment: Chris, in .NET you can do new List().
Comment: Also, in .NET, there is no ArrayList. That's why I edited your answer in the first place.
Comment: @Judah: Okay, fair point. I'm a Java developer, so I answered for Java, not .NET. In Java, `List` is an interface, so you cannot have instances of it.
Comment: Fair enough, we'll leave yours in place.
Answer: One thing I miss in C# from Java is the forced handling of checked exceptions. In C# is it far to common that one is unaware of the exceptions a method may throw and you're at the mercy of the documentation or testing to discover them. Not so in Java with checked exceptions.
Comment: I like the fact that C# does not have checked exceptions, but this was the first post to even point out the difference, so +1.
Comment: FWIW, this was considered in the dev't of C# and the rationale for leaving them out is real.  Consider http://www.artima.com/intv/handcuffs.html .  It's not that the designers are philosophically opposed to checked exceptions-- rather, they're waiting for a better technique that offers similar benefits without all the hairy drawbacks.
Comment: Yes, yes, a very contentious subject...
Answer: Java has autoboxing for primitives rather than value types, so although System.Int32[] is an array of values in C#, Integer[] is an array of references to Integer objects, and as such not suitable for higher performance calculations.
Comment: Pete: that's an excellent point, and one that I never knew or at least never considered (being a C# developer and new to Java).
Answer: No delegates or events - you have to use interfaces. Fortunately, you can create classes and interface implementations inline, so this isn't such a big deal
Comment: "isn't such a big deal".  Right.  Because `x => x.Foo` vs. `new Bar() { public void M(SomeType x) { return x.Foo; } }` -- who cares about code being 10 times shorter?
Answer: The built-in date/calendar functionality in Java is horrible compared to System.DateTime.  There is a lot of info about this here: What's wrong with Java Date & Time API?
Some of these can be gotchas for a C# developer:

The Java Date class is mutable which can make returning and passing dates around dangerous.
Most of the java.util.Date constructors are deprecated.  Simply instantiating a date is pretty verbose.
I have never gotten the java.util.Date class to interoperate well with web services.  In most cases the dates on either side were wildly transformed into some other date & time.

Additionally, Java doesn't have all the same features that the GAC and strongly-named assemblies bring.  Jar Hell is the term for what can go wrong when linking/referencing external libraries.
As far as packaging/deployment is concerned:

it can be difficult to package up web applications in an EAR/WAR format that actually install and run in several different application servers (Glassfish, Websphere, etc).
deploying your Java app as a Windows service takes a lot more effort than in C#.  Most of the recommendations I got for this involved a non-free 3rd party library
application configuration isn't nearly as easy as including an app.config file in your project.  There is a java.util.Properties class, but it isn't as robust and finding the right spot to drop your .properties file can be confusing
Answer: There are no delegates in Java. Therefore, aside from all the benefits that delegates bring to the table, events work differently too. Instead of just hooking up a method, you need to implement an interface and attach that instead.
Answer: One thing that jumps out b/c it's on my interview list is that there is no "new" keyword analogue in Java for method hiding and there fore no compiler warning "you should put new here". Accidental method hiding when you meant to override leads to bugs.
(edit for example)
Example, B derives from A (using C# syntax, Java behaves same way last I checked but does not emit compiler warning). Does A's foo get called, or B's foo? (A's gets called, probably surprising the dev who implemented B).
class A
{
public void foo() {code}
}

class B:A
{
public void foo() {code}
}

void SomeMethod()
{
A a = new B(); // variable's type is declared as A, but assigned to an object of B.
a.foo();
}

Comment: How do you accidentally hide an instance method in Java? they're either final - so no override or `new` method to intentionally hide - or non-final so overridden rather than hidden.
Comment: The difference here is that in Java all methods are virtual (so in Java, it will be B.foo() that gets called because Java always does dynamic method dispatch), whereas in C# methods are not virtual by default (and A.foo() would be called).  Many coding style checkers for Java will tell you that B should use the @Override annotation.
Comment: Added example. I haven't tested it in Java in a while, but the interview question originally came from the Java group from my previous company. In C#, the compiler warning usually means that people change the name. I admit it is pretty odd case, but OP seemed to be asking for things that would be potential hard to find problems when switching langs.
Answer: The most harrasing difference to me when I switch to java it's the string declaration.
in C# string (most of the time)
in Java String
It's pretty simple, but trust me, it makes you lose so much time when you have the habit to s not S !
Answer: Java doesn't have LINQ and the documentation is hell. User interfaces in Java are a pain to develop, you lose all the good things Microsoft gave us (WPF, WCF, etc...) but get hard - to - use, hardly documented "APIs".
Answer: The one issue I've run into so far when working with Java coming from C# is Exceptions and Errors are different.
For example you cannot catch an out of memory error using catch(Exception e).
See the following for more details:
why-is-java-lang-outofmemoryerror-java-heap-space-not-caught
Comment: It's intentionally designed that way to discourage the application programmer from catching it
Comment: Yeah I'm sure it was, but coming from the C# side, where all you have to worry about is catching exceptions, threw me for a loop :)
Answer: It's been so long since I've been in Java but the things I noticed right off the bat in application development was C# event model, C# drag and drop vs using Layout Managers in Swing (if your doing App dev), and exception handling with Java making sure you catch an exception and C# not required.
Answer: In response to your very direct question in your title:
"C# developers learning Java, what are the biggest differences one may overlook?"
A: The fact that Java is considerably slower on Windows.
Comment: I run OpenSuSE 64bit and Windows 7 64bit, and Eclipse IDE is ("seems") faster in Windows.  Is your answer based on personal observation or on actual benchmarks?