gcc 或 javac 首次启动速度慢
任何人都可以解释为什么在 Linux 中,当我在一段时间不活动后启动 gcc 或 javac 时,它们需要一段时间才能启动。后续调用速度要快得多。有没有办法保证始终快速启动? (这个要求可能看起来很奇怪,但对我来说是必要的)。顺便说一下Ubuntu。
Can anyone explain why in linux when I start gcc or javac after some time of inactivity it takes a while for them to start. Subsequent invocations are way faster. Is there a way to ensure quick startup always? (This requirement may seem strange, but is necessary in my case). Ubuntu by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最有可能的是,这是代码页出错所花费的时间。如果确实需要的话,有几种方法可以避免这种延迟。最简单的方法是定期运行
gcc
。另一种方法是将 gcc 安装到 RAM 磁盘上。另一种方法是列出所涉及的文件,然后编写一个简单的程序将所有这些文件锁定到内存中。您可以使用以下内容:
strace -f gcc *gcc 命令的其余部分* 2>&1 | grep 打开 | grep -v -- -1
使用典型的 GCC 使用方式的 GCC 命令行。
您会发现在那里打开了库和二进制文件。在文件中制作完整列表。然后编写一个调用
mlockall(MCL_FUTURE)
的程序,然后从文件中读取文件名。对于每个文件,将其mmap
到内存中并读取每个字节。然后让程序永远休眠(或直到被杀死)。这将产生强制内存中每个文件的每一页的效果。您应该检查所有这些文件的总大小,并确保它不是您实际拥有的内存量的很大一部分!
顺便说一下,曾经有一种叫做 粘性位 的东西可以做这样的事情。如果您的平台有机会支持它,只需在所有使用的文件上设置它即可。 (尽管传统上它会导致文件被保存为交换,这在现代系统上不会使事情变得更快。)
Most likely, it's the time it takes for code pages to fault in. There are a few ways to avoid this delay if you really have to. The simplest would be to run
gcc
periodically. Another would be to installgcc
to a RAM disk.Another approach would be to make a list of which files are involved and then write a simple program to lock all those files into memory. You can use something like:
strace -f gcc *rest of gcc command* 2>&1 | grep open | grep -v -- -1
Use a GCC command line that's typical of how you are using GCC.
You'll find libraries and binaries being opened in there. Make a full list in a file. Then write a program that calls
mlockall(MCL_FUTURE)
then reads in filenames from the file. For each file,mmap
it into memory and read each byte. Then have the program just sleep forever (or until killed).This will have the effect of forcing every page of every file in memory. You should check the total size of all these files and make sure it's not a significant fraction of the amount of memory you actually have!
By the way, there used to be something called a sticky bit that did something like this. If by some chance your platform supports it, just set it on all the files used. (Although it traditionally caused the files to be saved to swap, which on a modern system won't make things any faster.)