How SetupAttachment works?

from, this code is in the constructor:

USpringArmComponent* SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraAttachmentArm"));

How can unreal keep track of SpringArm? because SpringArm is declare here, it is not a class member.
Is it somehow make a copy of SpringArm and set it up with RootComponent?

As you guessed, the SpringArm is being attached to the RootComponent.
Since the you are inheriting from AActor, your child actor class already has a RootComponent and it does not need to be specified in its members.