Leo Technology Stack Leo Technology Stack
首页
  • Android
  • Web
  • SpringBoot
  • 数据库
  • Docker
  • Netty
  • KubeSphere
  • Linux
  • Android Framework
  • 开源库
思维
  • 面试
  • 投资理财
  • 杂事记录
  • 索引

    • 分类
    • 标签
    • 归档
  • 开源项目

    • Advance Markdown
    • AnLibrary (opens new window)

Leo

不知名的架构师
首页
  • Android
  • Web
  • SpringBoot
  • 数据库
  • Docker
  • Netty
  • KubeSphere
  • Linux
  • Android Framework
  • 开源库
思维
  • 面试
  • 投资理财
  • 杂事记录
  • 索引

    • 分类
    • 标签
    • 归档
  • 开源项目

    • Advance Markdown
    • AnLibrary (opens new window)
  • 目录页

  • 前端

  • 后端

  • Linux

  • thinking

  • interview

    • 面试锦集
    • LeetCode笔记

    • Java面试题

    • Android面试题

      • Android 面试题大纲
      • Java面试总结
        • JVM虚拟机
        • Java中引用类型有哪些?
        • 什么是方法重载,什么是方法重写,有什么区别?
        • 关键字final和static是怎么使用的
        • String、StringBuffer、Stringuilder的区别
      • Android 基础知识面试总结
    • 数据结构

  • notes
  • interview
  • Android面试题
2021-05-25

Java面试总结

# JVM虚拟机

参考 (opens new window)

控制参数

  • -Xms 设置堆的最小空间大小
  • -Xmx 设置堆的最大空间大小
  • -XX:NewSize设置新生代最小空间大小
  • -XX:MaxNewSize设置新生代最大空间大小
  • -XX:PermSize设置永久代最小空间大小
  • -XX:MaxPermSize设置永久代最大空间大小
  • -Xss设置每个线程的堆大小
  • 老年代空间大小 = 堆空间大小 - 年轻代空间大小

JVM
JVM内存区域总体分两类,heap(堆)区和非heap区

  • heap区
    堆区分为Young Gen(新生代)、Tenured Gen(老年代-养老区)。其中新生代又分为Eden Space(伊甸园)、Survivor Space(幸存者区)
  • 非heap区
    Code Cache(代码缓存区)、Perm Gen(永久代)、Jvm Stack(虚拟机栈)、Local Method Statck(本地方法栈)

# Java中引用类型有哪些?

https://www.cnblogs.com/liyutian/p/9690974.html
每种编程语言都有自己操作内存中元素的方式,例如C和C++里是通过指针,而Java中则通过引用(reference)

  • 强引用(Strong Reference)

Java默认的声明都是强引用,只要强引用还存在,垃圾回收器将永远不会回收被引用的对象,哪怕内存不足时,JVM也会直接抛出OOM;
如果想中断强引用与对象之间联系,可以将强引用赋值为null,这样,JVM会在合适时候回收对象垃圾。

Object obj = new Object(); // 只要obj还指向Object对象,Object对象就不会被回收
obj = null; //手动置null
1
2
  • 软引用(Soft Reference)
    软引用是用来描述一些非必需但仍然有用的对象。在内存足够的时候,软引用对象不会被回收,只有在内存不足时,系统则会回收软引用对象,如果回收了软引用对象后人然没有足够的内存,才会抛出内存溢出异常。
    常用来实现缓存技术,如图片缓存等
    private static void testSoftReference() {

        List<Object> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            byte[] buff = new byte[1024 * 1024];
            SoftReference<byte[]> sr = new SoftReference<byte[]>(buff);
            list.add(sr);
        }

        System.gc();

        for (int i = 0; i < list.size(); i++) {
            Object object = ((SoftReference)list.get(i)).get();
            System.out.println(object);
        }

        //结果只有最后一个不为null,其他皆为null
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  • 弱引用(Weak Reference)
    若引用的引用强度要比软引用要更弱一些,无论内存是否足够,只要JVM开始开机回收,那些被弱引用关联的的对象都会被回收

private static void testWeakReference() {
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            byte[] buff = new byte[1024 * 1024];
            WeakReference<byte[]> sr = new WeakReference<byte[]>(buff);
            list.add(sr);
        }

        System.gc();

        for (int i = 0; i < list.size(); i++) {
            Object object = ((WeakReference)list.get(i)).get();
            System.out.println(object);
        }

        //结果全为null,无论内存是否足够,只要JVm开始进行垃圾回收,那些被弱引用关联的对象都会被回收!

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  • 虚引用(Phantom Reference)

虚引用是最弱的一种引用关系,如果一个对象仅持有虚引用,那么它和没有任何引用一样,它随时可能会被回收。
虚引用必须和 ReferenceQueue 引用队列一起使用。

点击查看PhantomReference源码
/*
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package java.lang.ref;


/**
 * Phantom reference objects, which are enqueued after the collector
 * determines that their referents may otherwise be reclaimed.  Phantom
 * references are most often used to schedule post-mortem cleanup actions.
 *
 * <p> Suppose the garbage collector determines at a certain point in time
 * that an object is <a href="package-summary.html#reachability">
 * phantom reachable</a>.  At that time it will atomically clear
 * all phantom references to that object and all phantom references to
 * any other phantom-reachable objects from which that object is reachable.
 * At the same time or at some later time it will enqueue those newly-cleared
 * phantom references that are registered with reference queues.
 *
 * <p> In order to ensure that a reclaimable object remains so, the referent of
 * a phantom reference may not be retrieved: The {@code get} method of a
 * phantom reference always returns {@code null}.
 *
 * @author   Mark Reinhold
 * @since    1.2
 */

public class PhantomReference<T> extends Reference<T> {

    /**
     * Returns this reference object's referent.  Because the referent of a
     * phantom reference is always inaccessible, this method always returns
     * {@code null}.
     *
     * @return {@code null}
     */
    public T get() {
        return null;
    }

    /**
     * Creates a new phantom reference that refers to the given object and
     * is registered with the given queue.
     *
     * <p> It is possible to create a phantom reference with a {@code null}
     * queue, but such a reference is completely useless: Its {@code get}
     * method will always return {@code null} and, since it does not have a queue,
     * it will never be enqueued.
     *
     * @param referent the object the new phantom reference will refer to
     * @param q the queue with which the reference is to be registered,
     *          or {@code null} if registration is not required
     */
    public PhantomReference(T referent, ReferenceQueue<? super T> q) {
        super(referent, q);
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  • 引用对列(ReferenceQueue)

引用队列可以与软引用、弱引用以及虚引用一起配合使用,当垃圾回收器准备回收一个对象时,如果发现它还有引用,那么就会在回收对象之前,把这个引用加入到与之关联的引用队列中去。程序可以通过判断引用队列中是否已经加入了引用,来判断被引用的对象是否将要被垃圾回收,这样就可以在对象被回收之前采取一些必要的措施。

与软引用、弱引用不同,虚引用必须和引用队列一起使用。

# 什么是方法重载,什么是方法重写,有什么区别?

重载(OverLoad)
让类以统一的方式处理不同类型数据的一种手段,实际上表现就是多个具有不同的参数个数或者类型的同名函数,同时又存在于同一个类中,是一个类多态性的一种表现

重写(Override)
父类与子类之间的多态性,实质是对父类函数进行重新定义,如果在子类中定义某方法与其父类有相同的名称和参数则该方法被重写,不过子类函数的访问权限不能小于父类;
若子类方法与父类某一方法具有相同方法名、返回类型和参数列表,则新方法将覆盖原有的方法,如需父类中原有方法则可使用super关键字。

区别
重载和重写都是Java多态性的不同表现,
重写是父类和子类之间多态性的表现,在运行时起作用(动态多态性);
重载是一个类中多态的表现,在编译时起作用(静态多态性)

# 关键字final和static是怎么使用的

  • static 静态的,可用于修饰属性和方法
    • 作用于某个字段
    • 作用于方法
    • 不能应用于局部变量
    • Java禁止使用全局方法,所以一引入static方法通过类本身直接调用
  • final 不可改变,常应用于数据、方法和类

# String、StringBuffer、Stringuilder的区别

类|描述|是否可变|是否线程安全|多线程or单线程
---|---|---|--|
String|?|?|?|
StringBuffer||||
StringBuilder||||

编辑此页 (opens new window)
上次更新: 2022-04-28, 11:21:32
Android 面试题大纲
Android 基础知识面试总结

← Android 面试题大纲 Android 基础知识面试总结→

Theme by Leo | Copyright © 2016-2022 Leo | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式