가비지 컬렉션 방식은, 메모리 관리를 가비지 컬렉터라는 것이 프로그램 실행중에 동적으로 감시하고 있다가, 더이상 사용할 필요가 없다고 여겨지는 것을 메모리에서 삭제해 주는 것입니다. 즉, 실행타임에서 메모리 관리를 하는 것입니다.
그와는 달리, ARC는 프로그램이 실행되고 있는 상태에서 감시하는 것이 아니라, 코드를 빌드할 때에(컴파일할 때) 컴파일러가 프로그래머 대신에 release 코드를 적절한 위치에 넣어주는 것입니다.
이건 아주 중요한 장점인데, 가비지 컬렉션이라는 것이(대표적으로 Java나 .NET에서 사용됩니다.) 항상 메모리를 차지하고 감시해야기 때문에 프로그램 자체 외에 메모리 사용량이 더 늘어날 수 밖에 없으며, 지속적인 감시를 위해 CPU를 일부 사용할 수 밖에 없는데 비해, ARC는 어차피 수동으로 개발자가 넣을 코드를 컴파일러가 넣어주는 것이기 때문에, 전혀 그런 오버헤드가 필요 없다는 것입니다.
Tuesday, April 25, 2017
iOS GCD와 NSOperationQueue의 차이
GCD는 동시에 실행하려는 작업 단위를 대표 할 수있는 경량의 방법이다. 해당 작업 단위는 개발자가 직접 스케줄하지 않고 시스템이 스케줄 관리를 해준다. 블럭들 사이에서 의존성을 부여하는 것은 쉽지 않은 일이며, 작업 취소 혹은 일시정시 같은 일을 하기 위해서는 각 개발자가 개인별로 추가해야한다.
NSOperation과 NSOperationQueue는 GCD에 비해 추가적인 기능을 제공하며 여러 operation에 의존성을 부여할 수도 있다. 뿐만 아니라 재사용도 가능하며 취소 혹은 일시정지와 같은 기능도 가능하다. NSOperation은 KVO 기술을 완벽하게 사용할 수 있다. 그래서 NSOperation이 실행되기 시작하면 NSNotificationCenter를 통해 상태 변화에 대한 노티를 받을 수 있다.
NSOperation과 NSOperationQueue는 GCD에 비해 추가적인 기능을 제공하며 여러 operation에 의존성을 부여할 수도 있다. 뿐만 아니라 재사용도 가능하며 취소 혹은 일시정지와 같은 기능도 가능하다. NSOperation은 KVO 기술을 완벽하게 사용할 수 있다. 그래서 NSOperation이 실행되기 시작하면 NSNotificationCenter를 통해 상태 변화에 대한 노티를 받을 수 있다.
iOS 백그라운드 비동기 작업 방법 (3 가지)
1. PerformSelectorInXXX
self performSelectorInBackground:@selector(myMethod:) withObject:
self performSelectorInMainThread:@selector(updateUI:) withObject:
-(void)updateUI:(NSDictionary *)param{
}
2. NSOperationQueue
_queue = [[NSOperationQueue alloc] init];
//시간이 오래 걸리는 작업을 만들어 백그라운드 큐에 돌려줍시다.
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
[self heavyOperation];
//오래 걸리는 작업의 결과로 UI업데이트를 진행시켜줍니다.
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self updateUI:nil];
}];
}];
[_queue addOperation:operation];
3. GCD
// 여기서부터 비동기 코드 시작.
// dispatch_async 함수는 내부블럭의 코드 실행에 영향을 받지 않고 바로 실행이 끝난다.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
// 작업이 오래 걸리는 API를 백그라운드 스레드에서 실행한다.
BOOL res = [self heavyOperation];
dispatch_async(dispatch_get_main_queue(), ^{
// 이 블럭은 메인스레드(UI)에서 실행된다.
if (res) {
[self updateUI:nil];
}else {
[self alertFail];
}
});
});
dispatch_one : 한번만
dispatch_after : 일정 시간 후에
self performSelectorInBackground:@selector(myMethod:) withObject:
self performSelectorInMainThread:@selector(updateUI:) withObject:
-(void)updateUI:(NSDictionary *)param{
}
2. NSOperationQueue
_queue = [[NSOperationQueue alloc] init];
//시간이 오래 걸리는 작업을 만들어 백그라운드 큐에 돌려줍시다.
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
[self heavyOperation];
//오래 걸리는 작업의 결과로 UI업데이트를 진행시켜줍니다.
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self updateUI:nil];
}];
}];
[_queue addOperation:operation];
3. GCD
// 여기서부터 비동기 코드 시작.
// dispatch_async 함수는 내부블럭의 코드 실행에 영향을 받지 않고 바로 실행이 끝난다.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
// 작업이 오래 걸리는 API를 백그라운드 스레드에서 실행한다.
BOOL res = [self heavyOperation];
dispatch_async(dispatch_get_main_queue(), ^{
// 이 블럭은 메인스레드(UI)에서 실행된다.
if (res) {
[self updateUI:nil];
}else {
[self alertFail];
}
});
});
dispatch_one : 한번만
dispatch_after : 일정 시간 후에
EGL PBuffers vs Pixmaps differences
EGL에서 생성할수 있는 EGLSurface는 다음과 같이 3가지 종류이다.
1) Window Surface
- On-Screen Rendering Surfaces를 위해 사용
- 리눅스의 framebuffer에 해당 할 것이다
- eglCreateWindowSurface() 사용
2) Pbuffers Surface
- Off-Screen Rendering Surfaces를 위해 사용
- Graphic 메모리나 GPU 메모리에 생성
- eglCreatePbufferSurface() 사용
- Client(OpenGL ES)에서 생성한 Pbuffer를 EGL의 Pbuffer와 binding 할 때 사용은 eglCreatePbufferFromClientBuffer() 를 사용
3) Pixmaps Surface
- Off-Screen Rendering Surfaces 를 위해 사용하고
Graphic 메모리나 GPU 메모리에 생성한다는 점에서
Windows Surface와 다름
- OpenGLES 및 OpenVG와 같은 Client APIs 외의
APIs를 지원한다는 점에서 Pbuffer Surface와 다름
- Android의 TextureSurface에서 사용 (SurfaceTextureClient, SurafceTexureLayer 클래스)
Pixmaps는 PBuffers와 다르게 다른 Client APIs를 사용가능하다고 하는데 이는 buffer의 포인터가 노출이 되어 있어서 다른 클라이언트도 접근이 가능하다는 뜻이다. 예를 들어 C언어에서 포인터로 접근하여 직접 픽셀을 수정이 가능.
1) Window Surface
- On-Screen Rendering Surfaces를 위해 사용
- 리눅스의 framebuffer에 해당 할 것이다
- eglCreateWindowSurface() 사용
2) Pbuffers Surface
- Off-Screen Rendering Surfaces를 위해 사용
- Graphic 메모리나 GPU 메모리에 생성
- eglCreatePbufferSurface() 사용
- Client(OpenGL ES)에서 생성한 Pbuffer를 EGL의 Pbuffer와 binding 할 때 사용은 eglCreatePbufferFromClientBuffer() 를 사용
3) Pixmaps Surface
- Off-Screen Rendering Surfaces 를 위해 사용하고
Graphic 메모리나 GPU 메모리에 생성한다는 점에서
Windows Surface와 다름
- OpenGLES 및 OpenVG와 같은 Client APIs 외의
APIs를 지원한다는 점에서 Pbuffer Surface와 다름
- Android의 TextureSurface에서 사용 (SurfaceTextureClient, SurafceTexureLayer 클래스)
Pixmaps는 PBuffers와 다르게 다른 Client APIs를 사용가능하다고 하는데 이는 buffer의 포인터가 노출이 되어 있어서 다른 클라이언트도 접근이 가능하다는 뜻이다. 예를 들어 C언어에서 포인터로 접근하여 직접 픽셀을 수정이 가능.
Android OpenGL GLSurfaceView 예제
안드로이드에서 OpenGL ES를 사용하려면 가장 간단하게 GLSurfaceView를 사용하면 된다.
GLSurfaceView를 생성하고 GLSurfaceView.Renderer를 구현하면 기본적인 뼈대가 완성 된다.
아래의 예제는 화면을 빨간색으로 지우는 단순한 예제이다.
package com.duongame.opengl;
import android.opengl.GLSurfaceView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
GLSurfaceView glSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setRenderer(new MainRenderer());
setContentView(glSurfaceView);
//setContentView(R.layout.activity_main);
}
}
package com.duongame.opengl;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MainRenderer implements GLSurfaceView.Renderer {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(1,0,0,1);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}
}
GLSurfaceView를 생성하고 GLSurfaceView.Renderer를 구현하면 기본적인 뼈대가 완성 된다.
아래의 예제는 화면을 빨간색으로 지우는 단순한 예제이다.
package com.duongame.opengl;
import android.opengl.GLSurfaceView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
GLSurfaceView glSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setRenderer(new MainRenderer());
setContentView(glSurfaceView);
//setContentView(R.layout.activity_main);
}
}
package com.duongame.opengl;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MainRenderer implements GLSurfaceView.Renderer {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(1,0,0,1);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}
}
Subscribe to:
Posts (Atom)