How do I discover memory usage of my application in Android?

How can I find the memory used on my Android application, programmatically? I hope there is a way to do it. Plus, how do I get the free memory of the phone too? Note that memory usage on modern operating systems like Linux is an extremely complicated and difficult to understand area. In fact the chances of you actually correctly interpreting whatever numbers you get is extremely low. (Pret

如何在Android中发现我的应用程序的内存使用情况?

如何通过编程的方式找到我的Android应用程序中使用的内存? 我希望有办法做到这一点。 另外,我如何获得手机的免费记忆? 请注意,像Linux这样的现代操作系统的内存使用情况是一个非常复杂且难以理解的领域。 事实上,你实际上正确解释你得到的数字的机会是非常低的。 (几乎每次我与其他工程师一起查看内存使用量数字时,总会长时间讨论它们的实际含义,结果只会导致一个模糊的结论。) 注意:我们现在有更多关于管理

Java threads and number of cores

I just had a quick question on how processors and threads work. According to my current understanding, a core can only perform 1 process at a time. But we are able to produce a thread pool(lets say 30) with a larger number than the number of cores that we posses(lets say 4) and have them run concurrently. How is this possible if we are only have 4 cores? I am also able to run my 30 thread pro

Java线程和内核数量

我只是对处理器和线程如何工作提出了一个简短的问题。 根据我目前的理解,一个核心一次只能执行一个进程。 但是我们能够产生一个比我们拥有的核心数量更大的线程池(比如说30),并让它们同时运行。 如果我们只有4个内核,这怎么可能? 我也可以在本地计算机上运行30线程程序,并且还可以继续在计算机上执行其他活动,例如观看电影或浏览互联网。 我已经在某处看到线程调度发生,并且这种错觉给出了这4个核心同时运行这30

Is there a difference between concurrency and parallelism in java?

I have been doing some research in Google and cant quite get my head around the differences (if any) between concurrent and parallel programs in java. Some of the information I have looked at suggests no differences between both. Is this the case?? It depends on who is defining it. The people who created the Go programming language call code Concurrent if it is broken up into pieces which co

java中的并发和并行有区别吗?

我一直在Google做一些研究,并且不能完全理解Java中并发和并行程序之间的差异(如果有的话)。 我所看到的一些信息表明两者之间没有区别。 这是这种情况吗? 这取决于谁在定义它。 如果创建Go编程语言的人员将代码分解为可以并行处理的代码,则调用代码Concurrent,而并行意味着这些代码段实际上正在同时运行。 由于这些都是编程原则,编程语言对它们的定义没有任何影响。 但是,Java 8将具有更多的功能来实现并发性和并

Which goes on the stack or heap?

I am doing some studying and I came across a question that asks to show the correct memory diagram of the following code: int [] d1 = new int[5]; d1[0] = 3; Integer [] d2 = new Integer[5]; d2[0] = new Integer(3); ArrayList d3 = new ArrayList(); d3.add(3); Here is my attempt at a memory diagram, but it may be incorrect: I understand things like objects, instance variables, and "new"

哪一个会堆栈或堆?

我正在做一些学习,并且遇到了一个问题,要求显示以下代码的正确内存图: int [] d1 = new int[5]; d1[0] = 3; Integer [] d2 = new Integer[5]; d2[0] = new Integer(3); ArrayList d3 = new ArrayList(); d3.add(3); 这是我在内存图上的尝试,但它可能不正确: 我理解像对象,实例变量和“新”实例都在堆中,像局部变量和基本类型这样的东西都在堆栈中,但是对于数组类型,我仍然感到困惑。 任何帮助表示赞赏。 Java上

How can I test if an array contains a certain value?

I have a String[] with values like so: public static final String[] VALUES = new String[] {"AB","BC","CD","AE"}; Given String s , is there a good way of testing whether VALUES contains s ? Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of primitives (see the comments). Since java-8 You can now use a Stream to check whether an array of int , double o

我如何测试数组是否包含某个值?

我有一个String[] ,其值如下所示: public static final String[] VALUES = new String[] {"AB","BC","CD","AE"}; 给定String s ,是否有一种测试VALUES是否包含s的好方法? Arrays.asList(yourArray).contains(yourValue) 警告:这对于基元数组不起作用(请参阅注释)。 自java-8 您现在可以使用Stream来检查int , double或long数组是否包含值(分别使用IntStream , DoubleStream或LongStream ) 例 int[] a = {1,

What is an efficient way to implement a singleton pattern in Java?

什么是在Java中实现单例模式的有效方式? Use an enum: public enum Foo { INSTANCE; } Joshua Bloch explained this approach in his Effective Java Reloaded talk at Google I/O 2008: link to video. Also see slides 30-32 of his presentation (effective_java_reloaded.pdf): The Right Way to Implement a Serializable Singleton public enum Elvis { INSTANCE; private final String[] favoriteSongs

什么是在Java中实现单例模式的有效方式?

什么是在Java中实现单例模式的有效方式? 使用枚举: public enum Foo { INSTANCE; } Joshua Bloch在Google I / O 2008上的Effective Java Reloaded讲座中解释了这种方法:链接到视频。 另请参阅演示文稿的幻灯片30-32(effective_java_reloaded.pdf): 实现可串行化单例的正确方法 public enum Elvis { INSTANCE; private final String[] favoriteSongs = { "Hound Dog", "Heartbreak Hotel" };

When does static class initialization happen?

When are static fields initialized? If I never instantiate a class, but I access a static field, are ALL the static blocks and private static methods used to instantiate private static fields called (in order) at that instant? What if I call a static method? Does it also run all the static blocks? Before the method? A class's static initialization normally happens immediately before th

静态类初始化何时发生?

什么时候静态字段被初始化? 如果我从来没有实例化一个类,但我访问一个静态字段,所有的静态块和私有静态方法用于实例化在那个时刻调用(按顺序)的私有静态字段? 如果我调用静态方法会怎么样? 它是否也运行所有的静态块? 方法之前? 一个类的静态初始化通常在第一次发生以下事件之前发生: 该类的一个实例被创建, 调用该类的静态方法, 该类的静态字段被分配, 一个非恒定的静态字段被使用,或者 对于顶

Why doesn't Java allow overriding of static methods?

Why is it not possible to override static methods? If possible, please use an example. Overriding depends on having an instance of a class. The point of polymorphism is that you can subclass a class and the objects implementing those subclasses will have different behaviors for the same methods defined in the superclass (and overridden in the subclasses). A static method is not associated w

为什么Java不允许重写静态方法?

为什么不可能覆盖静态方法? 如果可能的话,请使用一个例子。 重写取决于有一个类的实例。 多态性的要点是,你可以继承一个类的子类,并且实现这些子类的对象对于在超类中定义的相同方法(并在子类中重写)将具有不同的行为。 静态方法不与任何类的任何实例关联,因此这个概念不适用。 推动Java设计的两个考虑因素影响了这一点。 其中一个问题是性能问题:Smalltalk对它的速度太慢了(垃圾回收和多态调用是其中的一部分

What is method hiding in Java? Even the JavaDoc explanation is confusing

Javadoc says: the version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invoked is the one in the subclass. doesn't ring a bell to me. Any clear example showing the meaning of this will be highly appreciated. public class Animal { public static void foo() { System.out.println("Animal"); } } public

什么是隐藏在Java中的方法? 即使是JavaDoc的解释也是令人困惑的

Javadoc说: 被调用的隐藏方法的版本是超类中的版本,并且被调用的重写方法的版本是子类中的版本。 不响铃给我。 任何清楚的例子显示这个意思将不胜感激。 public class Animal { public static void foo() { System.out.println("Animal"); } } public class Cat extends Animal { public static void foo() { // hides Animal.foo() System.out.println("Cat"); } } 在这里, Cat.fo

mocking a singleton class

I recently read that making a class singleton makes it impossible to mock the objects of the class, which makes it difficult to test its clients. I could not immediately understand the underlying reason. Can someone please explain what makes it impossible to mock a singleton class? Also, are there any more problems associated with making a class singleton? Of course, I could write something

嘲笑一个单身人士课程

我最近读到,制作一个单独的课程使得不可能嘲笑课程的对象,这使得测试它的客户变得很困难。 我无法立即明白其根本原因。 有人可以解释一下,嘲笑一个单身人士课程是不可能的吗? 此外,还有更多的问题与创建一个单独的类相关吗? 当然,我可以写一些不使用单例的东西,他们是邪恶的,使用Guice / Spring /不管怎样,但是首先,这不会回答你的问题,其次,当你使用遗留代码的时候,你有时不得不处理单例。例。 所以,我们