Development Notes
  • Introduction
  • Programming Langauges
    • Java
      • Cache
      • Java Fundamentals
      • Multithreading & Concurrency
      • Spring Boot
        • Spring Security
        • Development tips
      • ORM
        • Mybatis
      • Implementation & Testing
    • Node.js
      • Asynchronous Execution
      • Node.js Notes
    • Python
      • Memo
  • Data Structure & Algorithm
  • Database
  • Design Pattern
  • AWS Notes
    • Services
      • API Gateway
      • CloudHSM
      • Compute & Load Balancing
        • Auto Scaling Group
        • EC2
        • ECS
        • ELB
        • Lambda
      • Data Engineering
        • Athena
        • Batch
        • EMR
        • IoT
        • Kinesis
        • Video Streaming
        • Quicksight
      • Deployment
        • CloudFormation
        • Code Deploy
        • Elastic Beanstalk
        • OpsWorks
        • SAM
        • SSM
      • ElasticSearch
      • Identity & Federation
        • Directory Service
        • IAM
        • Organizations
        • Resource Access Manager (RAM)
        • SSO
        • STS
      • KMS
      • Management Tools
        • Catalog
        • CloudTrail
        • CloudWatch
        • Config
        • Cost Allocation Tags
        • GuardDuty
        • Savings Plans
        • Trusted Advisor
        • X-Ray
      • Migration
        • Cloud Migration: The 6R
        • Disaster Recovery
        • DMS
        • VM Migrations
      • Networking
        • ACM
        • CloudFront
        • Direct Connect
        • EIP & ENI
        • Network Security
        • PrivateLink
        • Route53
        • VPC
        • VPN
      • Service Commnucation
        • Amazon MQ
        • SNS
        • SQS
        • Step Functions
        • SWF
      • Storage
        • Aurora
        • DynamoDB
        • EBS
        • EFS
        • ElastiCache
        • RDS
        • Redshift
        • S3
        • Storage Gateway
      • Other Services
        • Alexa for Business, Lex, Connect
        • AppStream 2.0
        • CloudSearch
        • Comprehend
        • Data Tools
        • Elastic Transcoder
        • Mechanical Turk
        • Rekognition
        • WorkDocs
        • WorkSpaces
    • Well Architect Framework
      • Security
      • Reliability
      • Performance Effeciency
      • Cost Optimization
      • Operational Excellence
    • Labs
      • Webserver Implementation
      • ELB Implementation
      • Auto-scaling Implementation
      • A 3-tier Architecture In VPC
  • Architecture
    • Security
  • Spark
    • Memo
  • Conference Notes
    • Notes of JCConf 2017
  • AI Notes
Powered by GitBook
On this page

Was this helpful?

  1. Programming Langauges
  2. Java

Java Fundamentals

PreviousCacheNextMultithreading & Concurrency

Last updated 2 years ago

Was this helpful?

Memo

    • private

    • default (no modifier): within package

    • protected: within package and sub-classes

    • public

  • Special modifier

    • volatile: variables wouldn't be cached by threads, must be read / write within main memory.

    • transient: variables would be excluded during serialization / deserialization.

  • Enum

    • Constants can be initialized with one or more attributes with different types.

      • Override hashcode / equals for hash collections and lambda grouping.

    • Constant name / toString, enum valueOf are mentioning the constant.

Java 8 Features

  • Stream

    • Primitive Stream

      • Ex. IntStream

        • Method for iteration: range, rangeClosed

        • Using mapToInt(Class::getter).toArray() to assign to an int[]

        • Don't need to specify Comparator in min(), max()

    • Statistics method

      • Ex. average()

        • Using orElse() / getAsDouble() to set up a default value. (changing assignation from an OptionalDouble to a double)

      • summaryStatistics() encapsulates count, avg, sum, min, max.

    • findFirst() is deterministic, and findAny() is not.

    • Group By

      • One attribute

        • .collect(Collectors.groupingBy(Class::getterMethod))

      • Multiple attributes should be encapsulated into an tuple (DIY) object.

        • .collect(Collectors.groupingBy(object -> new Tuple(object.getter1(), object.getter2(), ..)

    • For callee to invoke other operations.

GC

  • Goals

    • Shorten full GC time

    • Let fewer instances to get into to old generation

  • Memory composition

    • Shared by threads

      • Heap (shared by threads, storing: non-primitive objects, class members, static variables)

        • Young Generation

          • Composition (suggested ratio - 8:1:1)

            • Eden

            • From Survivor

            • To Survivor

          • When Eden is full, trigger Minor GC to copy survivors into To Survivor, and release Eden, From Survivor. If To Survivor is full, instances get into the Old Generation.

          • Adjusted with -Xmn: (or separately with: -XX:NewSize, -XX:MaxNewSize)

        • Old Generation

          • When Old Generation is full, trigger Full GC to utilize Old Generation.

          • Old Generation space is: -Xmx subtracts with -Xmn

        • Heap is adjusted with: -Xms, -Xmx

      • Permanent (or called "Method area")

        • Storing classes, methods, constant pool

        • Adjusted with: -XX:PermSize, -XX:MaxPermSize

    • Owned by a thread, in Code Cache (or called "Native area") for threads.

      • Program counter

      • Natives

      • Stack (owned by each thread) to store:

        • Methods being called

        • Arguments being passed

        • Local primitive type variables, local references

      • Adjusted with: -Xss

  • Algorithms

    • mark and sweep (used in old generation of heap)

    • copying (used in young generation of heap)

    • mark and compact (used in old generation of heap)

  • Collectors

    • Serial

      • single threaded, mark and sweep (Effective but long Stop The World time)

    • Parallel

      • multi-threaded, mark and sweep (High throughput but long STW time)

    • CMS

      • concurrent mark and sweep (low STW, but consumes CPU)

      • steps

        • CMS inital mark (STW)

        • CMS concurrent mark

        • CMS remark (STW)

        • CMS concurrent sweep

    • G1

      • for low latency scenarios (replacement for CMS, suggested to use on machine with HEAP space of 6G up)

      • dividing heap into regions (1~32Mb)

  • Tricks

    • To create a memory dump when the the OutOfMemory error occurs.

      • Add: -XX:+HeapDumpOnOutOfMemoryError

  • Reference

Modifier Types
Optional
CompletableFuture
Thread states
Java 記憶體組成與參數、回收判斷、回收演算法
GC Collector差異與概念作法
如何优化Java GC
JVM 的内存管理与 GC 调优
Types of OutOfMemoryError
Tricks of the Trade: Tuning JVM Memory for Large-scale Services