简体   繁体   English

如何从生成的 Java class 文件中删除未使用的常量

[英]How to remove the unused constants from the generated Java class file

I often use constants in my Java programs, however when taking a look at a decompiled Java class file, I noticed that all the constant's uses throughout the program was replaced by their literal values, while the original declarations and assignments at the top of the class was still present, I'm wondering if these variables are just sitting there wasting memory since they're not being used anywhere in the file. I often use constants in my Java programs, however when taking a look at a decompiled Java class file, I noticed that all the constant's uses throughout the program was replaced by their literal values, while the original declarations and assignments at the top of the class仍然存在,我想知道这些变量是否只是坐在那里浪费 memory 因为它们没有在文件中的任何地方使用。 and if there's a way to restrict constants only to compile time and have them removed after the class file is generated.并且如果有办法将常量限制为仅编译时间并在生成 class 文件后将其删除。

The compiler is smart enough to use memory efficiently in this kind of case.编译器足够聪明,可以在这种情况下有效地使用 memory。 And, even let's suppose that you did have wasted memory per constant, let's do a thought experiment on how much 'space' that may be, realistically.而且,即使假设您确实浪费了每个常数的 memory,让我们对实际可能有多少“空间”进行思想实验。

Let's say we had a mixture of string, int, and other basic types.假设我们混合了字符串、int 和其他基本类型。 Each char in a string is 2 bytes in java, and an int is 4 bytes.在 java 中,一个字符串中的每个 char 是 2 个字节,一个 int 是 4 个字节。 Let's say we had 75,000 char constants and 25,000 int constants.假设我们有 75,000 个 char 常量和 25,000 个 int 常量。 That is 75k * 2 so 150k bytes or ~150 kilobytes of memory for the string constants For the ints that is 25k * 4 or 100k, so ~100 kilobyte of memory.对于字符串常量,即 75k * 2 即 150k 字节或约 150 KB 的 memory 对于 25k * 4 或 100k 的整数,则约 100 KB 的 memory。

So a total of ~250kb.所以总共〜250kb。

The most basic computer you can buy these days has a minimum of 4gb of ram, more likely 8gb to 16gb.这些天你能买到的最基本的电脑至少有 4gb 的内存,更有可能是 8gb 到 16gb。

So 250kb / 4000000kb * 100 = 0.00625% of the lowest spec modern computer.所以 250kb / 4000000kb * 100 = 0.00625% 的最低规格现代计算机。

Now if you are running some microcontroller or other limited hardware, it maaay be something, but even then it is doubtful and you probably aren't using java.现在,如果您正在运行一些微控制器或其他有限的硬件,它可能是一些东西,但即便如此它也是值得怀疑的,您可能没有使用 java。

As a general rule of thumb, compilers and interpreters tend to be pretty good at their job, and trusting them to do the right thing most of the time, for most programmers and programs is good enough.作为一般的经验法则,编译器和解释器往往很擅长他们的工作,并且相信他们在大多数时候做正确的事情,对于大多数程序员和程序来说已经足够好了。

There are times when certain patterns, such as massive creation of new objects, then triggering garbage collection or other issues like that can have large performance impacts.有时某些模式(例如大量创建新对象,然后触发垃圾收集或其他类似问题)可能会对性能产生很大影响。

The key is to know where actual bottlenecks and gotchya's are within a language/computer/use patterns.关键是要知道语言/计算机/使用模式中的实际瓶颈和问题。

It does get more complicated once we move into the distributed space and massive scale - but again unless you are working in that kind of environment, you generally don't need to consider it.一旦我们进入分布式空间和大规模,它确实会变得更加复杂 - 但除非你在那种环境中工作,否则你通常不需要考虑它。

Focus your expectations on your use case, and if you are optimizing for a 10 millisecond gain for something only a few people use, it's probably a waste of time - unless you are doing it for fun.将您的期望集中在您的用例上,如果您正在为只有少数人使用的东西优化 10 毫秒的增益,那可能是浪费时间 - 除非您这样做是为了好玩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM